C中不同定义的同名结构 [英] Same name structure with different definition in C

查看:35
本文介绍了C中不同定义的同名结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否允许在同一个项目中的 2 个不同的 c 文件中使用具有不同定义的相同名称结构.例如.

Is it allowed to use same name structure with different definitions in 2 different c files in the same project. For eg.

文件 1.c

typedef struct
{
    unsigned int unVar;             

} abc;

File2.c

typedef struct
{
    int var;
} abc;

abc 用于这两个文件.当我将这些文件编译为同一项目的一部分时,没有错误,但我想了解这是否正确使用.

abc is used in both the files. When i compile these file as part of same project there are no errors, but i want to understand whether this is correct usage.

推荐答案

6.7.2.1 结构和联合说明符

6.7.2.1 Structure and union specifiers

  1. struct-or-union-specifier 中 struct-declaration-list 的存在声明了一个新类型,翻译单元内.

类型仅在翻译单元中定义,在本例中为 .c 文件.

Types are defined only within a translation unit, a .c file in this case.

在两个不同的翻译单元中定义两个同名类型没有问题.

There is no problem with defining two types with the same name in two different translation units.

然而,除非它们遵循 6.2.7., p1 中描述的规则,否则这两种类型是不兼容的.您定义的类型不兼容.

However those two types are not compatible unless they follow the rules described in 6.2.7., p1. The types you defined are not compatible.

这篇关于C中不同定义的同名结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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