c - 如何在 C 中使用指针打印带有结构的变量?

标签 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;
}

最佳答案

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

感谢大家的回答!

关于c - 如何在 C 中使用指针打印带有结构的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33311998/

相关文章:

C:使用 sprintf 和 strncpy 将数据插入指针数组

c - 使用c程序以交叉方式打印字符串

c++ - 通过重载运算符+添加2个指针导致无限循环c++

c++ - 返回指针 c

正确使用头文件?

c - 如何在C中打印枚举变量?

c - C中的简单循环代码

c - 在 C 中使用 strcpy 函数的优点

c++ - 从 ‘char*’到 ‘char’的无效转换[-fpermissive]如何打印?但是在变量里面

c - malloc/calloc 分配一个已经分配的地址