Danny Wilson
Danny Wilson

Reputation: 332

How to use pointers to print variables with structs in C?

My exercise is to use a structure to initialise values and then print them (sounds easy enough) but instead of using the structure to print them, we have to use the pointer *p. I know this probably has a really simple answer, but help would be appreciated!

#include <stdio.h>
#include <string.h>

struct info
{
    int total;
    char *str;
};

int main()
{

    struct info s, *p = &s;

    s.total = 50;
    s.str = "hello";

    printf("Info total: %i\n", s.total);
    printf("Info *str: %s\n", s.str);

    return 0;
}

Upvotes: 1

Views: 57

Answers (1)

Danny Wilson
Danny Wilson

Reputation: 332

s.total <=> p->total or (*p).total

Thanks to you all for the answers!

Upvotes: 3

Related Questions