【发布时间】:2017-12-19 23:26:23
【问题描述】:
我试图通过引用将自定义类型对象传递给函数,但我不知道我可能做错了什么。我阅读了How do you pass a typedef struct to a function? 以及其他参考资料,并且可以发誓我已经在这样做了。我清除了我正在做的所有其他事情,甚至这个斯巴达代码也会引发 5 个错误。帮助我,Stackexchange;你是我唯一的希望!
目标只是能够改变对象中数组中的值。
#include <stdio.h>
#include <math.h>
typedef struct structure {
char byte[10];
char mod;
} complex;
void simpleInit (complex *a, char value) {//put the value in the first byte and zero the rest
a.byte[0] = value;
char i;
for (i = 1; i < 10; ++i) {
a.byte[i] = 0;
}
a.mod = 1;
}
void main () {
complex myNumber;
char value = 6;
simpleInit (myNumber, value);
}
当我尝试运行它时,我得到了这个错误和 4 个类似的错误:
test2.c:10:3: 错误:在不是结构或联合的东西中请求成员“字节”
a.byte[0] = 值;
【问题讨论】:
-
a.byte-->a->byte -
simpleInit期望complex *作为其第一个参数。myNumber的类型为complex,而不是complex *。您需要将&myNumber传递给simpleInit。它与typedef无关。这适用于任何基本的 C 数据类型。 -
你需要学习语言
标签: c arrays reference typedef