c - 使用 extern 声明数组时指定其大小有什么不同吗?

标签 c arrays declaration

例如 这两种说法有什么区别吗:

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/

相关文章:

javascript - 通过嵌套数组进行过滤

objective-c - 如何从我的 appDelegate 访问我的 viewController? ( swift )

c# - 将 C 迁移到 C# 问题!

c - 为什么我的指针类型不兼容,但代码仍然有效?

c - 在 1 个循环中存储 char n 次

c - 数组C编程中打印出奇怪的字母

python - 你如何创建一个 numpy 垂直排列?

c++ - 在没有new关键字的情况下创建的c++中结构对象的范围

c# - 非静态字段、方法或属性需要对象引用

c - 当套接字设置为非阻塞时,有什么方法可以在阻塞模式下临时使用 send 或 recv