定义类型x
和一个数组X
那种类型的。
x.h:
typedef struct _x {int p, q, r;} x;
extern x X[];
单独的文件来保存巨大的喇叭阵列X
.
x.c:
#include "x.h"
x X[] = {/* lotsa stuff */};
现在我想用X
:
main.c:
#include "x.h"
int main()
{
int i;
for (i = 0; i < sizeof(X)/sizeof(x); i++) /* error here */
invert(X[i]);
return 0;
}
main.c
不会编译;错误是:
error: invalid application of ‘sizeof’ to incomplete type ‘struct x[]’
我如何获得尺寸X
无需对其进行硬编码?
In x.h
add:
extern size_t x_count;
In x.c
add:
size_t x_count = sizeof(X)/sizeof(x);
然后使用变量x_count
在你的循环中。
除法必须在包含数组初始值设定项的编译单元中完成,因此它知道整个数组的大小。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)