简体   繁体   中英

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;
}
s.total <=> p->total or (*p).total

谢谢大家的答案!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM