This question already has answers here:
Closed 3 years ago.
What does “request for member '*******' in something not a structure or union” mean?
(6个答案)
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为整数。

最佳答案

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

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

asFirst_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/

10-11 23:10
查看更多