C形式结构声明 [英] C style struct declaration

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

问题描述

我有关于C风格的结构的一个简单的问题。我是通过一些示例code挖掘和发现下列方式声明一个结构:

  typedef结构_STRUCTNAME
{
   //结构内容
} STRUCTNAME;

请注意,缺乏第二次下划线的STRUCTNAME显示出来。我的理解是,这将宣布所谓STRUCTNAME单_STRUCTNAME这种结构中没有更多的对象可以被实例化。

然而,这似乎不是这种情况。这种类型的结构从未实际上是在code实例,除了在一个地方:在这种对象的全局阵列在随机的地方使用:

  const的结构STRUCTNAME arrayName中的[] =
{
   //各种STRUCTNAMEs这里声明
};

再次注意缺少下划线的(我认为是实例化对象的名字吗?)

我的理解是完全关闭?

有人能解释一下吗?


解决方案

  typedef结构_STRUCTNAME
{
   //结构内容
} STRUCTNAME;

这件code的做了两件事:


  1. 定义一个名为结构结构_STRUCTNAME

  2. 创建一个的typedef 所谓的结构 STRUCTNAME 的。

这样做的原因是,在适当的C code,你的方式,否则声明结构(像上面的)将以下内容:

 结构_STRUCTNAME structInstance;

不过,与的typedef 在的地方,你可以简单地使用下列内容:

  STRUCTNAME structInstance;

同为枚举的声明也是如此。

I have a quick question about C style struct's. I was digging through some sample code and found a struct declared in the following way:

typedef struct _STRUCTNAME
{
   // struct contents
} STRUCTNAME;

Please note the lack of an underscore on the second time STRUCTNAME shows up. My understanding was that this would declare a single _STRUCTNAME called STRUCTNAME and no more objects of this struct could be instantiated.

However, this does not seem to be the case. A struct of this type was never actually instantiated in the code except in one place: in a global array of such objects that was used in random places:

const struct STRUCTNAME ARRAYNAME[] = 
{
   // various STRUCTNAMEs declared here
};

Note the lack of an underscore again (which I thought was the name of the instantiated object?)

Is my understanding completely off?

Could someone explain?

解决方案

typedef struct _STRUCTNAME
{
   // struct contents
} STRUCTNAME;

This piece of code does two things:

  1. Defines a structure called struct _STRUCTNAME and
  2. Creates a typedef of that structure called STRUCTNAME.

The reason for this is that in proper C code, the way you would otherwise declare a struct (like the above) would be the following:

struct _STRUCTNAME structInstance;

However, with the typedef in place, you can simply use the following:

STRUCTNAME structInstance;

The same holds true for enum declarations.

这篇关于C形式结构声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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