当前位置:网站首页>Using typedef in C language to change the name of data type
Using typedef in C language to change the name of data type
2022-07-28 20:15:00 【Nansensen】
typedef keyword
In addition to direct use C The name of the data type provided by the language ( Such as int,char,float,double etc. ) And the structure type name defined by ourselves , You can also use typedef Specify a new data type name to replace the existing type name . let me put it another way ,typedef Is that Alias existing data types . for example :
#include <stdio.h>
typedef int Integer; // to int Took a person named Integer, You can use Integer To define shaping variables
typedef char * String; // to char * Took a person named String, You can use String To define character pointer variables
int main()
{
Integer i = 10; // Equate to int i = 10;
String p; // Equate to char * p;
return 0;
}
#include <stdio.h>
typedef struct Student
{
char name[100];
int age;
float score;
}ST, * PST; // to struct Student It's a nickname ST, to struct Student * A man named PST
int main()
{
ST stu; // Equate to struct Student stu;
PST pStu; // Equate to ST * pStu; Also equivalent to struct Student * pStu;
return 0;
}
It should be noted that ,C The original structure name and use are allowed in the language typedef The renamed structure has the same name , for example :
#include <stdio.h>
typedef struct Student
{
char name[100];
int age;
float score;
}Student; // At this time Student Is equivalent to struct Student
int main()
{
Student stu; // Equate to struct Student stu;
return 0;
}
- Why rename data types ?
- The first is to facilitate programmer programming , For example, when we define structural variables , If you always have to write first
structThen write the structure name , Defining structural variables becomes very tedious , Write it every timestruct, And it's easy to forget to write code , And the code will be cumbersome , If we can use a few short letters to represent a long structure name , Then why don't we do it ? - The second is the universality of the whole program , If the variables that a program initially stores data are integers , What is naturally defined in the program are integer variables , But if you want to change all defined data variables to floating-point type at some time , The workload is predictable , But there are
typedefTo help. , It only takes one step to finish . Because the first thing we want to store is integer data , Then we can put int Rename it asElemType( Of course, you can choose any name , Here we takeElemTypeIt's just convenient to use in data structure ), namely ,typedef int ElemType;, When we want to change the variables corresponding to these data to floating point , We just need to do thistypedef float ElemType;, Compared with modifying the types of variables one by one , UsetypedefIt's really easy ! - There's more , In order to achieve Code is comment Purpose , Great programmers often rename data types .
- If you don't quite understand why this is the case , You can leave an impression in your mind first , Come back and analyze it carefully when you need to use this part , You can knock more and watch more , In fact, in the process of programming, you will gradually understand the benefits of doing so .
边栏推荐
- 【实验分享】CCIE—BGP反射器实验
- Two methods to judge the size end
- A chip company fell in round B
- Andorid system layout, values, drawable adaptation
- Solve flask integration_ Error reporting in restplus
- Return and job management of saltstack
- Multi-Modal Knowledge Graph Construction and Application: A Survey
- 9. Pointer of C language (1) what is pointer and how to define pointer variables
- 跨区域网络的通信学习静态路由
- Can China make a breakthrough in the future development of the meta universe and occupy the highland?
猜你喜欢

WFST decoding process

Saltstack advanced

河北:稳粮扩豆助力粮油生产提质增效

私有化部署的即时通讯平台,为企业移动业务安全保驾护航

Reverse string

English translation Italian - batch English translation Italian tools free of charge

3、 Are formal and actual parameters in a programming language variables?

The cloud native programming challenge is hot, with 510000 bonus waiting for you to challenge!

数字图像理论知识(一)(个人浅析)

What is the variance?
随机推荐
Const pointer of C language and parameter passing of main function
9. Pointer of C language (2) wild pointer, what is wild pointer, and the disadvantages of wild pointer
2022年下半年系统集成项目管理工程师认证8月20日开班
6. Functions of C language, why functions are needed, how functions are defined, and the classification of functions
私有化部署的即时通讯平台,为企业移动业务安全保驾护航
C+ + core programming
Zfoo adds routes similar to mydog
[experiment sharing] CCIE BGP reflector experiment
[C language] string reverse order implementation (recursion and iteration)
7. Functions of C language, function definitions and the order of function calls, how to declare functions, prime examples, formal parameters and arguments, and how to write a function well
Solve flask integration_ Error reporting in restplus
What is the process of swing event processing?
Stories of Party members | Li qingai uses cartoons to drive farmers to increase income and become rich
Basic usage of docker
Kubeedge releases white paper on cloud native edge computing threat model and security protection technology
C+ + core programming
WFST decoding process
ssm中项目异常处理
BeanFactory not initialized or already closed - call ‘refresh‘ before accessing beans via the Applic
adb remount of the / superblock failed: Permission denied