当前位置:网站首页>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 .
边栏推荐
- 1. C language variable type, global variable, local variable
- Array method added in ES6
- MySQL命令语句(个人总结)
- 通配符 SSL/TLS 证书
- Power Bi 2021 calendar DAX code
- Can China make a breakthrough in the future development of the meta universe and occupy the highland?
- Item exception handling in SSM
- Usage of const and assert
- JVM (24) -- performance monitoring and tuning (5) -- Analyzing GC logs
- 【CodeForces】Educational Codeforces Round 132 (Rated for Div. 2)
猜你喜欢

2022年下半年系统集成项目管理工程师认证8月20日开班

Function fitting based on MATLAB

Kubeedge releases white paper on cloud native edge computing threat model and security protection technology

Saltstack advanced

熊市下PLATO如何通过Elephant Swap,获得溢价收益?

Item exception handling in SSM

English Translation Spanish - batch English Translation Spanish tools free of charge

河北邯郸:拓展基层就业空间 助力高校毕业生就业

How can Plato obtain premium income through elephant swap in a bear market?

为什么客户支持对SaaS公司很重要?
随机推荐
云原生编程挑战赛火热开赛,51 万奖金等你来挑战!
[C language] initial C language reflection and summary
MySQL命令语句(个人总结)
软考中级(系统集成项目管理工程师)高频考点
Tencent cloud deployment lamp_ Experience of building a station
HSETNX KEY_ Name field value usage
English translation Arabic - batch English translation Arabic tools free of charge
一文读懂如何部署具有外部数据库的高可用 K3s
English Translation Spanish - batch English Translation Spanish tools free of charge
[in depth study of 4g/5g/6g topics -44]: urllc-15 - in depth interpretation of 3GPP urllc related protocols, specifications and technical principles -9-low delay technology -3-non slot scheduling mini
Use of strtok and strError
mmo及时战斗游戏中的场景线程分配
[experiment sharing] CCIE BGP reflector experiment
flask_ Mail source code error
[C language] string reverse order implementation (recursion and iteration)
local/chain/run_ tdnn.sh:
Why is customer support important to SaaS?
Using Lex (Flex) to generate lexical analyzer of PL language
C language - pointer
BeanFactory not initialized or already closed - call ‘refresh‘ before accessing beans via the Applic