C语言 类型定义/结构声明

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18806392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:26:44  来源:igfitidea点击:

Typedef/struct declarations

cstructtypedef

提问by user2780061

What is the difference between these two declarations, if someone could explain in detail:

这两个声明有什么区别,如果有人能详细解释一下:

typedef struct atom {
  int element;
  struct atom *next;
};

and

typedef struct {
  int element;
  struct atom *next;
} atom;

回答by Gangadhar

This is Normal structure declaration

这是正常的 structure declaration

  struct atom {
      int element;
      struct atom *next;
    };    //just declaration

creation of object

的制作 object

 struct atom object; 


  struct atom {
      int element;
      struct atom *next;
    }object;    //creation of object along with structure declaration


And

This is Type definition of struct atomtype

这是类型定义 struct atom类型

typedef  struct atom {
  int element;
  struct atom *next;
}atom_t;  //creating new type

Here atom_tis alias for struct atom

这里atom_t是别名struct atom

creation of object

对象的创建

atom_t object;      
struct atom object; //both the ways are allowed and same

回答by Barmar

The purpose of typedefis to give a name to a type specification. The syntax is:

的目的typedef是为类型规范命名。语法是:

typedef <specification> <name>;

After you've done that, you can use <name>much like any of the built-in types of the language to declare variables.

完成此操作后,您可以<name>像使用该语言的任何内置类型一样来声明变量。

In your first example, you the <specification>is everything starting with struct atom, but there's no <name>after it. So you haven't given a new name to the type specification.

在您的第一个示例中,您的<specification>一切都以 开头struct atom,但<name>后面没有。所以你没有给类型规范一个新的名字。

Using a name in a structdeclaration is not the same as defining a new type. If you want to use that name, you always have to precede it with the structkeyword. So if you declare:

struct声明中使用名称与定义新类型不同。如果要使用该名称,则始终必须在其前面加上struct关键字。所以如果你声明:

struct atom {
    ...
};

You can declare new variables with:

您可以使用以下命令声明新变量:

struct atom my_atom;

but you can't declare simply

但你不能简单地声明

atom my_atom;

For the latter, you have to use typedef.

对于后者,您必须使用typedef.

Note that this is one of the notable differences between C and C++. In C++, declaring a structor classtype doesallow you to use it in variable declarations, you don't need a typedef. typedefis still useful in C++ for other complex type constructs, such as function pointers.

请注意,这是 C 和 C++ 之间的显着差异之一。在 C++ 中,声明 astructclass类型确实允许您在变量声明中使用它,您不需要typedef. typedef在 C++ 中对于其他复杂类型构造仍然很有用,例如函数指针。

You should probably look over some of the questions in the Relatedsidebar, they explain some other nuances of this subject.

您可能应该查看相关侧栏中的一些问题,它们解释了该主题的其他一些细微差别。

回答by Deep

The general syntax of typedef keyword will be: typedef existing_data_type new_data_type;

typedef 关键字的一般语法为: typedef existing_data_type new_data_type;

typedef struct Record {
    char ename[30];
     int ssn;
    int deptno;
} employee;