当前位置:网站首页>Specific differences between typedef and define
Specific differences between typedef and define
2022-07-28 05:00:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack , I wish every programmer can learn more languages .
1) #define It's preprocessing instructions , 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 . such as : #define PI 3.1415926 In program :area=PI*r*r Will be replaced by 3.1415926*r*r Suppose you put #define The number in the sentence 9 Write in letters g Pretreatment is also brought into .
2)typedef Is processed at compile time . It gives an alias to an existing type in its own scope , But You cannot use the typedef specifier inside a function definition.
3)typedef int * int_ptr; And #define int_ptr int * The function is to use int_ptr representative int * , But the two are different , As mentioned earlier ,#define In pretreatment Make a simple replacement when , and typedef Not a simple replacement , Instead, a type is declared in the same way as a variable . in other words ;
//refer to (xzgyb( Old Dharma )) #define int_ptr int * int_ptr a, b; // amount to int * a, b; Just a simple macro replacement
typedef int* int_ptr; int_ptr a, b; //a, b All point to int The pointer to ,typedef by int* Introduced a new mnemonic
This also explains why the following view holds //QunKangLi( The maintenance cost is proportional to the square of the creativity of the programmer ) typedef int * pint ; #define PINT int *
that : const pint p ;//p Non modifiable , but p The content pointed to can be changed const PINT p ;//p Changeable , But p The content pointed to cannot be changed .
pint It's a pointer type const pint p Just lock the pointer p Non modifiable and const PINT p yes const int * p The lock is the pointer p Object referred to .
3) Perhaps you have noticed #define It's not a statement Don't put a semicolon at the end of the line , otherwise It will be replaced by a semicolon .
Turn to another article
One 、typedef How to use
stay C/C++ In language ,typedef It is often used to define an identifier and keyword Another name for , It's part of the language compilation process , But it doesn't actually allocate memory space , Examples are like :
typedef int INT;typedef int ARRAY[10];typedef (int*) pINT;
typedef It can enhance the readability of the program , And the flexibility of identifiers , But it also has “ Non intuition ” Etc .
Two 、#define How to use
#define Define a statement for a macro , It is often used to define constants ( Including no parameter and with parameter ), And to achieve that “ It's kind on the surface 、 There's a long string of ” The macro , It's not in the process of compiling itself , But before that ( Pretreatment process ) It's over , But it is also difficult to find potential errors and other code maintenance problems , Its examples are like :
#define INT int#define TRUE 1#define Add(a,b) ((a)+(b));#define Loop_10 for (int i=0; i<10; i++)
stay Scott Meyer Of Effective C++ The terms of a Book 1 There are about #define Analysis of the disadvantages of sentences , And good alternatives , You can see .
3、 ... and 、#typedef And #define The difference between
From the above concept, we can basically understand ,typedef Just to add � A new name for the identifier for readability ( Just an alias ), and #define Originally in C To define constants , here we are C++,const、enum、inline The emergence of makes it gradually become an alias tool . Sometimes very easy I don't know the relationship with typedef Which of the two is better , Such as #define INT int This kind of sentence , use typedef It can also be completed , Which one is better ? I advocate using typedef, Because in the early days of many C This statement in the compiler is illegal , Just today's compilers Expanded again . In order to be as compatible as possible , Generally follow #define Definition “ Can be read ” And the task of some macro statements , and typedef Is often used to define keyword、 Lengthy type of distinction name .
Macro definitions are simply string substitutions ( In situ expansion ), and typedef Instead of expanding in situ , Its new name has a certain encapsulation , So that the newly named identifier has the function of defining variables more easily . Look at the third line of the first big point code above :
typedef (int*) pINT; And the following line :#define pINT2 int*
The effect is the same ? It's different ! There are differences in practice :pINT a,b; The effect is the same as int *a; int *b; Indicates that two integer pointer variables are defined . and pINT2 a,b; The effect is the same as int *a, b; Indicates that an integer pointer variable is defined a And integer variables b.
Be careful : The other end of the line ; The difference between numbers !
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/118209.html Link to the original text :https://javaforall.cn
边栏推荐
- POJ 2763 housewife wind (tree chain partition + edge weighting point weight)
- FreeRTOS startup process, coding style and debugging method
- Automated test tool playwright (quick start)
- What is the reason why the easycvr national standard protocol access equipment is online but the channel is not online?
- 面试了一位38岁程序员,听说要加班就拒绝了
- 塑料可以执行GB/T 2408 -燃烧性能的测定吗
- 猿辅导技术进化论:助力教与学 构想未来学校
- Printf() print char* str
- HDU 1435 stable match
- excel实战应用案例100讲(十一)-Excel插入图片小技巧
猜你喜欢

Driving the powerful functions of EVM and xcm, how subwallet enables Boca and moonbeam

linux下安装mysql
![[idea] check out master invalid path problem](/img/83/d36362ba314177cd6f1f74f3e922cd.png)
[idea] check out master invalid path problem

Histogram of pyplot module of Matplotlib (hist(): basic parameter, return value)

C语言ATM自动取款机系统项目的设计与开发

Youxuan database participated in the compilation of the Research Report on database development (2022) of the China Academy of communications and communications

go-zero单体服务使用泛型简化注册Handler路由

How to send and receive reports through outlook in FastReport VCL?

How to quickly locate bugs? How to write test cases?

Use and expansion of fault tolerance and fusing
随机推荐
Domain name (subdomain name) collection method of Web penetration
Angr(十一)——官方文档(Part2)
Clickhouse填坑记2:Join条件不支持大于、小于等非等式判断
HDU 3078 network (lca+ sort)
数据安全逐步落地,必须紧盯泄露源头
Basic knowledge of network security - password (I)
[function document] torch Histc and paddle Histogram and numpy.histogram
Program life | how to switch to software testing? (software testing learning roadmap attached)
[Hongke technology] Application of network Multimeter in data center
Improving the readability of UI layer test with puppeter
Dynamic SQL and paging
Depth traversal and breadth traversal of tree structure in JS
The difference between alter and confirm, prompt
驾驭EVM和XCM的强大功能,SubWallet如何赋能波卡和Moonbeam
C语言ATM自动取款机系统项目的设计与开发
What SaaS architecture design do you need to know?
机器人教育在STEM课程中的设计研究
alter和confirm,prompt的区别
Use animatedbuilder to separate components and animation, and realize dynamic reuse
FreeRTOS startup process, coding style and debugging method