C language TYPEDEF usage details

xiaoxiao2021-03-14  189

In C language, there is often the following usage:

Typedef struct tag {...} mytype, * myptr;

Once later, you can define the Struct tag structure with mytype.

In the past, I used TypeDef, and I found a misunderstanding according to its literal. For example, I want to define myint as the int type, the code I use is as follows:

Typedef myint int;

I understand the above code as: Define myint type INT. Everything seems to be a natural thing, but it knows when compiling.

In fact, when declaring variables in C language, there is a storage-class-specifier, which includes our familiarity.

ExternStaticAutoregister

When the storage type indicator does not specify the storage type indicator, the compiler will automatically extract the value according to the convention. In addition, the location of the storage type indicator is also arbitrary (but required before the variable name and pointer *), that is, the following lines of code are equivalent:

Static const INT I;

Const static int i;

Int const static i;

Const int stat I;

According to the C language specification, the TypeDef and Storage Type indicators are equivalent! So, we replace the above-mentioned places to typedef:

Typedef const INT i;

Const typef INT I;

INT const typef i;

Const int TypedEf i;

The semantics of the above code are: Define I as a type name, its equivalent type is const INT. In the future, if we have I a; code, it is equivalent to Const Int A;

The same is true for a pointer, such as:

INT const typef * t;

So code

T P;

Be equivalent to

INT const * p;

In addition, Typedef cannot be used simultaneously with Static and other storage type indicators because each variable can only have a storage type, so code:

Typedef static int i;

It is illegal.

转载请注明原文地址:https://www.9cbs.com/read-129402.html

New Post(0)