【发布时间】:2014-06-07 10:46:29
【问题描述】:
定义类型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 的大小?
【问题讨论】:
-
如果 x 是一个结构,你通常必须指定 sizeof(struct x)
-
typedef不需要使用struct
标签: c arrays compiler-errors sizeof