当前位置:网站首页>Can't use typedef yet? C language typedef detailed usage summary, a solution to your confusion. (learning note 2 -- typedef setting alias)

Can't use typedef yet? C language typedef detailed usage summary, a solution to your confusion. (learning note 2 -- typedef setting alias)

2022-06-13 01:47:00 It's Beichen bupiacra

Preface :
If you are learning C Language without knowing where to start , You can study with me C Language , During the winter vacation, I will post a blog every day , There are all kinds of C The knowledge of language , If you want to learn , Want to improve , Come and punch in with me every day , I hope we can make progress together .

One 、 Format the type alias

Set the type alias , Is to give the data type a new name , It's like giving people “ nickname ” equally .typedef Is given to the existing type A new name , Instead of creating new types . in order to “ See the name and know the meaning ”, Please try to use clear identifiers , And try to capitalize .
Set the format of the type alias to :

typedef  The original type   New type name ;

1. Alias common types

typedef int DATATYPE;
DATATYPE a;

It is equivalent to

int a;

2. Alias the structure type

For example, the name of a structure is stu, If you want to define a structure variable, you have to write it like this :

struct stu stu1;

struct It seems superfluous , If struct stu Make up an alias STU, It's much easier to write . This way of writing is more concise , The meaning is also very clear , Whether in standard header files or later programming practices , Will make extensive use of this alias .

typedef struct stu STU;
STU stu1;

3. Alias array types

typedef char ARRAY20[20];

Express ARRAY20 It's the type char[20] Another name for , It's a length of 20 Character array type of , Then you can use ARRAY20 Define an array :

ARRAY20 a1,a2,a3,a4;

It is equivalent to :

char a1[20], a2[20], a1[20], a2[20];

4. Alias pointer types

int *a;

Equivalent to

typedef int *STRING;
STRING a;

5. Alias array pointer types

typedef int (*PTR_TO_ARR)[4];

Express PTR_TO_ARR It's the type int (*) [4] Another name for , It is a two-dimensional array pointer type . Then you can use PTR_TO_ARR Define a two-dimensional array pointer :

PTR_TO_ARR p1, p2;

6. Alias function pointer types

typedef int (*MyFUN)(int a,int b);
int Max(int a,int b);
MyFUN pMyFun;
pMyFun= Max;

After setting the type alias , The new type name has the same data type as the original type name , Users can choose to use the new type name and the original type name at will , The effect is the same , So what are the benefits of setting type aliases ? The purpose of aliasing is not to improve the efficiency of the program , But for coding convenience .

Two 、 Benefits of setting type aliases

1. Simplified long type name

C In language , Some data types have long names , It is easy to make mistakes in writing . If you set a short alias for such a data type , Can reduce the mistakes in writing . for example

typedef unsigned int uint;

adopt typedef to unsigned int Type to set a short alias uint, The following code needs to use unsigned int The place of , have access to uint, Is it more convenient , There are fewer characters in the type alias , Writing will naturally decrease .

2. Easy to maintain code ( Platform independence )

For example, we give short Type to set an alias “DATATYPE”, Use this alias in your code :

typedef short DATATYPE;

After a while , Find out short The type is too small , Not enough use , Want to change to a larger type int, Then just change where you set the type alias :

typedef int DATATYPE;

Now all in the code DATATYPE They are all expressed as int Type . Is it convenient ? If the type alias is not set , You will find the places to be modified in the code one by one , For a program with a large amount of code, the amount of work is very large .

3. Extension type information

In the code that holds someone's basic information , If you see one int Variable of type , How can we know what this variable represents ? It's human height 、 Weight or age ? You can give int Type sets the type alias :

typedef int age;

In the code , Variable types use age, In this way, it can be understood at once that this variable stores a person's age .

3、 ... and 、typedef And #define The difference between

#define( Macro definition ) yes Preprocessing instruction , Just a simple string substitution ( In situ expansion ), It's not in the process of compiling itself , But before that ( Pretreatment process ) We're done . Make a simple replacement when compiling preprocessing , Do not check for correctness , It doesn't matter whether the meaning is correct or not , Only when compiling the expanded source program will possible errors be found and reported .

typedef stay Compile time To deal with the . It gives an alias to an existing type in its own scope , Is a new name for the identifier to increase readability ( Just an alias ), Its new name has a certain encapsulation , So that the newly named identifier has the function of defining variables more easily , It's part of the language compilation process , But it doesn't actually allocate memory space .

Generally follow #define Definition “ Can be read ” Of Constant As well as some Macro statement The task of , and typedef Is often used to define keyword 、 Lengthy type Another name for .

typedef Is the statement ( With ’;‘ ending ), and #define It's not a statement ( Don't to ’;' ending )

原网站

版权声明
本文为[It's Beichen bupiacra]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280549162585.html