C:允许将任何数组分配给指向不完整类型数组的指针

标签 c arrays ansi language-lawyer incomplete-type

以下内容在 C89 中的合法性似乎有问题。我不明白为什么允许这样做。我正在阅读标准和我的 K&R2 副本,但我仍然不明白。

char Arr[16];
char (*Durr)[] = &Arr; /*Why is this allowed?*/

原来如此。我需要引用 C89 标准来告诉我为什么允许这样做。这不是 C++ 问题,在那里绝对是非法的。谢谢!

编辑:这解释了标准中出现问题的地方:http://port70.net/~nsz/c/c89/c89-draft.html#3.3.16.1

最佳答案

char (*Durr)[] = &Arr; 中的初始化要求Durr 指向与Arr 类型兼容的类型数组>.

根据“6.7.6.2 数组声明符”(n1570)

6 For two array types to be compatible, both shall have compatible element types, and if both size specifiers are present, and are integer constant expressions, then both size specifiers shall have the same constant value.

因为 Durr 指向的数组有一个不完整的类型,这意味着这两个类型应该是兼容的,那么编译器不应该为这个初始化给出错误/警告。

关于C:允许将任何数组分配给指向不完整类型数组的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23486949/

相关文章:

C 堆栈指向地址?

java - 检查控制台是否支持 Java 中的 ANSI 转义码

c - 将 sprintf 与 char * 一起使用

php - 使用 PHP 将数组插入 MySQL

windows - 查找 WndProc 地址

html - CGI 不会通过 c (Eclipse) 中的 HTML 显示变量

C 编程 - "Stack around variable was corrupted"

c - 如何并行化 Windows 消息循环和 Hook 回调逻辑及其创建的 Ruby 线程?

c - 段错误 : 11 on c program

c - 在 C 中实现 FIFO 列表