Several common ways of structure definition

Reprinted from: https://blog.csdn.net/ly666888555/article/details/52206973

Thanks to the original author!

Everyone knows that if the struct is followed by a name, it is the name of the structure.

Six structure definitions are given below, the first of which is the most basic structure definition, which defines a structure A.

struct A            //第一种
{
    int a;
};

The second is to define a variable m of a structure B while defining a structure B.

struct B            //第二种
{
    int b;
}m;
  • The third structure definition does not give the name of the structure, but defines a variable n of the structure, that is to say, if you want to define the variable of the structure elsewhere, it is not possible, only the variable n It is necessary to define variables at the same time as defining the structure.
struct              //第三种
{
    int c;
}n;

  • The fourth structure definition adds the keyword typedef on the basis of the first structure definition. At this time, we regard struct D{int d} as a data type, but because no alias is given, it is directly defined with D Variables don't work. Such as D test;, the variable test cannot be directly defined in this way. But struct D test; works.
typedef struct D    //第四种
{
    int d;
};
  • The fifth structure definition adds an alias x to the fourth structure definition. At this time, as stated in the fourth structure definition, the structure E at this time has an alias x, so you can use x defines the structure variable of E. E cannot be directly defined, and struct needs to be added in front, such as struct E test;.
typedef struct E    //第五种
{
    int e;
}x;

The sixth type of structure definition is based on the fifth type minus the structure name, but it is also possible to use y directly to define the variable of the structure type. Such as y test;.

typedef struct      //第六种
{
    int f;
}y;

Because it is also not good at learning, if there are mistakes or omissions in some places, I would like to point out, thank you very much.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326575785&siteId=291194637