打印struct的成员,该成员是指向另一个struct的指针?

时间:2016-05-26 17:59:53

标签: 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)但是我收到了这个错误:

  

[错误]请求非结构或联合的成员'exponent'

我做错了什么?

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

1 个答案:

答案 0 :(得分:1)

您需要通过指针运算符(->

使用结构和联合成员访问
 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.

相关问题