c - 打印一个结构成员,该成员是指向另一个结构的指针?

标签 c struct printf

<分区>

typedef struct Monom{
    int coeficient;
    int exponent;
    struct Monom* Next;
    }Monom;

typedef struct list_polinom{
    struct Monom* First_element;
    }list_polinom;

int main(){
    struct list_polinom* Polinoms;
    struct Monom* Monoms;
    Polinoms = (struct list_polinom*)malloc( x * sizeof(struct list_polinom));
    Monoms = (struct Monom*)malloc(y * sizeof(stuct Monom));
    Polinoms[0].First_element = &Monoms[z];
    Monoms[z].exponent = x;
    return 0;
    }

所以我想打印 printf("%d\n",Polinoms[0].First_element.exponent) 但是我得到了这个错误:

[Error] request for member 'exponent' in something not a structure or union

我做错了什么?

注意:x, y ,z 为整数。

最佳答案

您需要通过指针运算符(->)使用结构和 union 成员访问

 printf("%d\n",Polinoms[0].First_element->exponent);
                                       ^^

因为 First_element 是指针类型。

也就是说,please see this discussion on why not to cast the return value of malloc() and family in C. .

关于c - 打印一个结构成员,该成员是指向另一个结构的指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37468010/

相关文章:

c++ - 包含相同类型的结构指针的结构指针的初始化

c++ - printf 的格式化缓冲区在哪里?

c - 调试传递两个参数的 C 程序

c - 尝试访问结构中的字符串会导致段错误

c - 在函数中返回结构,编译错误

c - 简单的 C 程序未打印

c - %s 格式用于查找字符串的地址

C while 循环额外迭代

c - C 代码在 Minix 系统上运行的进程数

检查输入是否存在可能的 Int 溢出