如何使用指针在C中使用结构打印变量?

时间:2015-10-23 21:57:47

标签: c pointers

我的练习是使用一个结构来初始化值,然后打印它们(声音很容易),但不是使用结构来打印它们,我们必须使用指针* p。我知道这可能有一个非常简单的答案,但帮助将不胜感激!

#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;
}

1 个答案:

答案 0 :(得分:3)

asInvoker

感谢大家的答案!