当前位置:网站首页>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 .
Set type alias typedef
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 )
边栏推荐
- [MathType] use MathType to output latex style formula
- Magics 23.0如何激活和使用视图工具页的切片预览功能
- TensorFlow 2.x 多显卡分布式训练
- [soft test] software designer knowledge points sorting (to be updated)
- Network communication tcp/ip
- JSON and protobuf Any interchange
- 【官方文件汇总】国科大学位论文撰写规范
- 移动IPv6光猫登录的一般ip地址账号与密码,移动光猫变桥接模式
- Run Presto under docker to access redis and Bi presentation
- VI keyboard diagram
猜你喜欢
numpy多维数组转置transpose
csdn涨薪技术之Jmeter接口测试数据库断言的实现与设计
[wsl2]wsl2 migrate virtual disk file ext4 vhdx
Devaxpress Chinese description --tdximageslider (picture rotation control)
What is Google plus large text ads? How to use it?
白噪声的详细理解
General IP address, account and password of mobile IPv6 optical cat login, and mobile optical cat is in bridging mode
LabVIEW大型项目开发提高质量的工具
Alertwindowmanager pop up prompt window help (Part 1)
【MathType】利用MathType输出LaTex样式的公式
随机推荐
Introduction to ROS runtime
Qt实现思维导图功能(二)
LabVIEW大型项目开发提高质量的工具
Design of distributed game server
Compiling minicom-2.7.1 under msys2
水管工游戏
This of phaser3 add. add. image
如何通过受众群体定位解决实际问题?
leetcode743. Network latency (medium, Dijkstra)
Temporary objects and compilation optimization
[soft test] software designer knowledge points sorting (to be updated)
Crypto JS reports uglifyjs error
Using OpenCV in go
如何利用您的自有数据来实现营销目标?
Alertwindowmanager pop up prompt window help (Part 1)
Explanation and application of prefix sum (one-dimensional, two-dimensional)
3、 Upload fabric photos to SQL server and provide name to display fabric photos
Super complete regular expressions
[pytorch FAQ] numpy:dll load failed while importing_ multiarray_ Umath: the specified module could not be found.
STM32 3*3矩阵按键(寄存器版本)