基本上我的代码中有类似的东西
struct connComp {
struct connComp *parent;
struct connComp *neigh[noNN];
int *pSpinValue, SpinValue, Flag;
unsigned int size;
} comp[N];
当我尝试通过编写 使用结构数组
在我的代码中,我从 gcc 编译器收到以下错误:comp[N]
作为 void function(struct connComp)
类型的函数的输入时function(comp)
incompatible type for argument 1 of ‘function’ function(comp)
expected ‘struct connComp’ but argument is of type ‘struct connComp *’
所以看起来 comp[N]
被声明为指针,我真的不明白为什么。预先非常感谢您的帮助!
最佳答案
将指针和数组传递给函数和 C 是等效的。 comp
是一个由 N 个 connComp
结构体组成的数组。当您将 comp
传递给函数时,您将获得数组开头的地址,其行为方式与指针相同。要在需要该结构的函数中使用 comp
,您必须取消引用该指针 - 尝试传入 comp[0]
。
关于c - 在 C 中声明结构数组时的奇怪之处,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50398705/