例如 这两种说法有什么区别吗:
extern char a[];
extern char a[4];
如果 a 的真实定义(在另一个源文件中)怎么办
char a[5];
但不是
char a[4];
最佳答案
extern int a[]
声明 a
为未指定大小的 int
数组,并被视为“不完整类型” (C.11 §6.7.6.2 ¶4)。不完整类型是指没有足够的信息来确定其大小的类型(C.11 §6.2.5 ¶1)。使用 extern
意味着该名称具有“外部链接”(C.11 §6.2.2 ¶4)。程序中对具有外部链接的相同名称的所有引用均指同一对象 (C.11 §6.2.2 ¶2)。
如果您有 extern int a[4]
,但它在其他地方定义为 int a[5]
,那么这将导致未定义的行为(C.11 §6.2.7¶2):
All declarations that refer to the same object or function shall have compatible type; otherwise, the behavior is undefined.
关于c - 使用 extern 声明数组时指定其大小有什么不同吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18223843/