The difference between typedef and #define

typedef aliases the type, use; end,

#define is just a simple macro replacement for pre-compilation processing instructions.

#define pint int*

typedef int * Pint;

const pint a,b;

const PRINT a,b;

Compare the difference

 

 

1) #define is a preprocessing instruction. It is simply replaced during compilation and preprocessing. It is not checked for correctness, regardless of whether the meaning is correctly brought in. Only when the source program that has been expanded is compiled will possible errors be found and Report an error. For example: #define PI 3.1415926 In the program: area = PI * r * r will be replaced with 3.1415926 * r * r. If you write the number 9 in the #define statement as the letter g, the preprocessing is also brought in. 2) The 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 * use int_ptr to represent int *, but the two are different. As mentioned earlier, #define performs simple replacement during preprocessing, and typedef is not a simple replacement, but It is to declare a type in the same way as defining variables. That is to say; // refer to (xzgyb (Old Bodhidharma)) #define int_ptr int * int_ptr a, b; // equivalent to int * a, b; is just a simple macro replacement typedef int * int_ptr; int_ptr a, b ; // a, b are all pointers to int, and typedef introduces a new mnemonic for int *. This also explains why the following point holds true // QunKangLi (maintenance cost is proportional to the square of the programmer's creativity) typedef int * pint; #define PINT int * Then: const pint p; // p cannot be changed, but the content pointed to by p can be changed const PINT p; // p can be changed, but the content pointed to by p cannot be changed. Pint is a pointer type const pint p is to lock the pointer p can not be changed and const PINT p is const int * p lock is the object pointed to by the pointer p. 3) You may have noticed that #define is not a statement. Do not add a semicolon at the end of the line, otherwise it will be replaced with a semicolon. Another article I. Usage of typedef In the C / C ++ language, typedef is commonly used to define an alias for an identifier and a keyword. It is part of the language compilation process, but it does not actually allocate memory space. Examples are: typedef int INT; typedef int ARRAY [10]; typedef (int *) pINT; typedef can enhance the readability of the program and the flexibility of the identifier, but it also has shortcomings such as "non-intuitive". Second, the usage of # define # define is a macro definition statement, which is usually used to define constants (including no parameters and with parameters), as well as to implement those macros that are "seemingly kind and long behind," which does not It is not carried out during the compilation process, but it has been completed before this (preprocessing process), but it is also difficult to find potential errors and other code maintenance problems. Its examples are: #define INT int # define TRUE 1 # define Add (a, b) ((a) + (b)); # define Loop_10 for (int i = 0; i <10; i ++) In Article 1 of Scott Meyer's Effective C ++, there is an analysis of the disadvantages of the #define statement, as well as good alternative methods, you can refer to it. Third, the difference between #typedef and #define can be basically clear from the above concept, typedef is just a new name for the identifier to increase readability (just an individual name), and #define was originally in C In order to define constants, in C ++, the emergence of const, enum, and inline has gradually become an alias tool. Sometimes it is easy to figure out which one is better to use with typedef. Statements such as #define INT int can be done with typedef. Which one is better? I advocate using typedefs because this statement is illegal in many early C compilers, but the current compilers have expanded. In order to be compatible as much as possible, generally follow the task of #define to define "readable" constants and some macro statements, while typedef is commonly used to define keywords and aliases of lengthy types. Macro definition is simply string substitution (in-place expansion), while typedef is not in-place expansion. 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 code above: typedef (int *) pINT; and the following line: #define pINT2 int * Same effect? It's different! See the difference in practice: the effect of pINT a, b; is the same as int * a; int * b; means that two integer pointer variables are defined. The effect of pINT2 a, b; is the same as int * a, b; means that an integer pointer variable a and an integer variable b are defined. Note: There is also a line ending; the difference between the numbers!
————————————————
Copyright Statement: This article is an original article by CSDN blogger "21aspnet", following the CC 4.0 BY-SA copyright agreement, please attach the original source link and this statement for reprint .
Original link: https://blog.csdn.net/21aspnet/article/details/6723915

 

Guess you like

Origin www.cnblogs.com/pquan/p/12730000.html