为什么联合中的成员名称可以与 C 中的 typedef 名称相同? [英] why could member names in a union be the same as typedef names in C?

查看:44
本文介绍了为什么联合中的成员名称可以与 C 中的 typedef 名称相同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在用于 8 位 MCU 应用程序的 SiLabs IDE 随附的compiler_defs.h"中找到了此定义.我做了一些搜索工作,它似乎与名称空间和标识符可见性问题有关.C 中使用了 5 个命名空间(参考:命名空间):

  1. 语句标签
  2. 结构、联合和枚举标记
  3. 结构或工会的成员
  4. 普通标识符
  5. Typedef 名称

一个例子是:

struct student {字符学生[20];整数类;内部标识;} 学生;

我对上面的例子没问题,但我仍然对下面的代码感到困惑,那么为什么 typedef 名称可以与联合定义中的成员名称相同?你能解释一下吗?谢谢.

来自compiler_defs.h"的代码摘录:

typedef unsigned char U8;typedef unsigned int U16;typedef unsigned long U32;typedef 有符号字符 S8;typedef 有符号整数 S16;typedef 有符号长 S32;typedef 联合 UU16{U16 U16;//[typedef name] [成员变量名]S16 S16;U8 U8[2];S8 S8[2];} UU16;typedef 联合 UU32{U32 U32;S32 S32;UU16 UU16[2];U16 U16[2];S16 S16[2];U8 U8[4];S8 S8[4];} UU32;

创建我自己的 main.c 并包含 compiler_defs.h:

U16 U16;//main.c 中的这一行导致编译器错误:在 VS2015 中重新定义,为什么?

解决方案

好吧,你回答了你自己的问题:

<块引用>

  1. 语句标签
  2. 结构、联合和枚举标记
  3. 结构或工会的成员
  4. 普通标识符
  5. Typedef 名称

这些是单独的命名空间,即一个名称中的名称不会与另一个名称中的相同名称冲突.在你的情况下:

U16 U16;//[typedef name] [成员变量名]

第一个 U16 位于列表中的命名空间 #5 中.第二个在命名空间#3 中.独立的命名空间,所以名称可以相同而不冲突.

I found this definition below in the "compiler_defs.h" that comes with SiLabs IDE for 8-bit MCU applications. I have done a little search work, it seems to be relevant to name spaces and identifier visibility issue. There are 5 name spaces used in C (ref: Name Spaces):

  1. Statement labels
  2. Structure, union, and enumeration tags
  3. Members of structures or unions
  4. Ordinary identifiers
  5. Typedef names

and an example is:

struct student {
    char student[20];
    int class; 
    int id; 
    } student; 

I am OK with the above example, but I am still confused about the code below, so why could the typedef names be the same as the member names in a union definition? Would you please shed some light on this? Thanks.

Code excerpt from "compiler_defs.h":

typedef unsigned char U8;
typedef unsigned int U16;
typedef unsigned long U32;

typedef signed char S8;
typedef signed int S16;
typedef signed long S32;

typedef union UU16
{
   U16 U16; // [typedef name] [member variable name]
   S16 S16;
   U8 U8[2];
   S8 S8[2];
} UU16;

typedef union UU32
{
   U32 U32;
   S32 S32;
   UU16 UU16[2];
   U16 U16[2];
   S16 S16[2];
   U8 U8[4];
   S8 S8[4];
} UU32;

Create my own main.c and include compiler_defs.h:

U16 U16; // this line in main.c cause compiler error: redefinition in VS2015, WHY?

解决方案

Well you sort of answered your own question:

  1. Statement labels
  2. Structure, union, and enumeration tags
  3. Members of structures or unions
  4. Ordinary identifiers
  5. Typedef names

These are separate namespaces, i.e. a name in one won't clash with the same name in another one. In your case:

U16 U16; // [typedef name] [member variable name]

The first U16 is in namespace #5 in your list. The second is in namespace #3. Separate namespaces, so the names can be the same without conflict.

这篇关于为什么联合中的成员名称可以与 C 中的 typedef 名称相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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