gpt4 book ai didi

c - 包含的结构定义无法识别

转载 作者:行者123 更新时间:2023-11-30 15:45:08 25 4
gpt4 key购买 nike

这一定是我的教授声明 typedef 的方式,因为我还没有遇到这个问题。

我有以下(一 block )头文件,以及使用它的随附代码:

多项式.h -

#ifndef POLYNOMIAL_H_
#define POLYNOMIAL_H_

struct term {
int coef;
int exp;
struct term *next;
};

typedef struct term term;

typedef term * polynomial;

#endif

多项式.c -

#include "polynomial.h"

void display(polynomial p)
{

if (p == NULL) printf("There are no terms in the polynomial...");

while (p != NULL) {

// Print the term
if (p.exp > 1) printf(abs(p.coef) + "x^" + p.exp);
else if (p.exp == 1) printf(abs(p.coef) + "x");
else printf(p.coef);

// Print the sign of the next term, if it applies
if (p.next != NULL) {
if (p.next->coef > 0) printf(" + ");
else printf(" - ");
}
}
}

但是每次我尝试访问该结构的任何属性时都会得到以下信息:

错误:请求非结构或 union 中的成员“exp”

包含、定义和所有内容 - 我只是在 C 中使用结构和 typedef 做了类似的事情,但我没有使用以下语法:typedef term * polynomial;。我想这可能是导致问题的原因,并让我失望。

如果我无法访问结构体的成员,如 p.expp.coefp.next,如何处理我可以吗?

PS - typedef term * polynomial; 到底是什么意思?我想象“多个项组成一个多项式”,但我不明白对 term 的访问如何变化。

最佳答案

polynomial 被类型定义为指向 term 的指针。您不能使用 . 访问其元素。您必须先取消引用它:

(*p).next

或使用箭头运算符:

p->next

关于c - 包含的结构定义无法识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19211737/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com