什么是申报一个C结构的语法正确方法是什么? [英] What's the syntactically proper way to declare a C struct?

查看:124
本文介绍了什么是申报一个C结构的语法正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过C结构之前声明几种不同的方式。这是为什么,什么,如果有的话,请问各做不同?

例如:

 结构foo的{
  短;
  INT B:
  浮℃;
};typedef结构{
  短D组;
  INTê;
  浮F;
}栏;typedef结构_baz {
  短;
  INT B:
  浮℃;
}巴兹;INT主(INT ARGC,字符常量*的argv [])
{
  结构foo的;
  杆B;
  巴兹℃;  返回0;
}


解决方案

好吧,明显的区别是体现在你的

 结构foo的;
杆B;
巴兹℃;

第一个声明是联合国 - 的typedef ED 结构,需要在结构关键字使用。第二个是一个的typedef ED匿名结构,所以我们使用的typedef 名称。第三个结合了第一和第二:你的例子使用巴兹(这是方便短),但可以很容易地使用结构_baz 达到同样的效果。

更新:<一href=\"http://stackoverflow.com/questions/4698600/whats-the-syntactically-proper-way-to-declare-a-c-struct/4698618#4698618\">larsmans'答案提到,你必须至少使用 A更常见的情形结构X {} 来使一个链表。第二种情况是不可能在这里(除非你放弃理智,并使用无效* 代替),因为结构是匿名的,而的typedef 不会发生,直到结构的定义,让你没有办法让一个(类型安全的)指针结构类型本身。第一个版本用于该用途的工作正常,但第三个一般为$以我的经验pferred p $。给他一些代表为。

一个更微妙的区别是在命名空间中的位置。在C,结构标签被放置在一个单独的命名空间与其他的名字,但的typedef 的名字都没有。所以下面是合法的:

 结构试验{
  //内容
};结构测试*测试(){
  //内容
}

但下面的不是,因为这将是暧昧什么名字测试是:

  typedef结构{
  //内容
}测试;测试*测试(){
  //内容
}

的typedef 使得短名称(总是一个加号),但将其放在同一个名字空间的变量和函数。通常这不是一个问题,但它超越了简单的缩短一个微妙的差异。

I've seen C structs declared several different ways before. Why is that and what, if anything, does each do different?

For example:

struct foo {
  short a;
  int b;
  float c;
};

typedef struct {
  short d;
  int e;
  float f;
} bar;

typedef struct _baz {
  short a;
  int b;
  float c;
} baz;

int main (int argc, char const *argv[])
{
  struct foo a;
  bar b;
  baz c;

  return 0;
}

解决方案

Well, the obvious difference is demonstrated in your main:

struct foo a;
bar b;
baz c;

The first declaration is of an un-typedefed struct and needs the struct keyword to use. The second is of a typedefed anonymous struct, and so we use the typedef name. The third combines both the first and the second: your example uses baz (which is conveniently short) but could just as easily use struct _baz to the same effect.

Update: larsmans' answer mentions a more common case where you have to use at least struct x { } to make a linked list. The second case wouldn't be possible here (unless you abandon sanity and use a void * instead) because the struct is anonymous, and the typedef doesn't happen until the struct is defined, giving you no way to make a (type-safe) pointer to the struct type itself. The first version works fine for this use, but the third is generally preferred in my experience. Give him some rep for that.

A more subtle difference is in namespace placement. In C, struct tags are placed in a separate namespace from other names, but typedef names aren't. So the following is legal:

struct test {
  // contents
};

struct test *test() {
  // contents
}

But the following is not, because it would be ambiguous what the name test is:

typedef struct {
  // contents
} test;

test *test() {
  // contents
}

typedef makes the name shorter (always a plus), but it puts it in the same namespace as your variables and functions. Usually this isn't an issue, but it is a subtle difference beyond the simple shortening.

这篇关于什么是申报一个C结构的语法正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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