应该定义和声明是否匹配? [英] Should definition and declaration match?

查看:105
本文介绍了应该定义和声明是否匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我.h文件中我有

extern int a[4];

在我的.c文件我有

int a[10];

那么,有没有任何问题,这件事?

So are there any issues with this?

宣言和定义大小事务?不是吧?

Declaration and definition size matters? Not right?

如果我写的的sizeof(A)中的文件之一,会是怎样的输出?
这是不确定的行为?

If I write sizeof(a) in one of the files, what will be the output? Is this undefined behavior?

推荐答案

如果您在您的源代码头文件文件的两个声明 A 必须具有的的类型为C说:

If you include your header file in your source file the two declarations of a must have the same type as C says:

(C11,6.7p4)在同一范围内的所有声明指向同一对象或函数应规定兼容的类型。

(C11, 6.7p4) "All declarations in the same scope that refer to the same object or function shall specify compatible types."

即使这两个声明有两种翻译单位,他们需要具备的相同的类型:

Even if the two declarations are in two translation units, they need to have the same type:

(C11,6.2.7p2)是指同一对象或函数的所有声明应具有兼容的类型;否则,其行为是未定义

(C11, 6.2.7p2) "All declarations that refer to the same object or function shall have compatible type; otherwise, the behavior is undefined."

这篇关于应该定义和声明是否匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆