I. Usage of typedef
____ In C/C ++, typedef is often 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 as follows:
Typedef int;
Typedef int array [10];
Typedef (int *) pint;
____ Typedef can be enhancedProgramBut it also has some disadvantages such as "non-intuitive.
Ii. # define usage
____ # Define is a macro definition statement. It is usually used to define constants (including non-parameters and parameters) and macros used to implement "surface kindness, long strings behind, it is not carried out in the compilation process, but completed before this (preprocessing process), but it is difficult to find potential errors and otherCodeMaintenance problem. Its instance is like:
# Define int
# Define true 1
# Define add (A, B) (a) + (B ));
# Define loop_10 for (INT I = 0; I <10; I ++)
____ In Clause 1 of Scott Meyer's Objective C ++, we have an analysis on the disadvantages of # define statements and a good alternative method. For more information, see.
Iii. Differences between typedef and # define
____ The above concepts can also be quite clear. typedef is just a new name (just an alias) for the identifier to increase readability ), # define was originally used to define constants in C. The emergence of C ++, const, Enum, and inline gradually turned it into an alias tool. Sometimes it is easy to figure out which one should be used for typedef and which one should be used, for example, a statement such as # define int, which one can be used for the same purpose? I advocate the use of typedef, because in many early C compilers, this statement was invalid, but today's compiler has expanded. To be as compatible as possible, # define is generally followed to define "readable" constants and macro statement tasks, while typedef is often used to define keywords and lengthy type aliases.
____ Macro definition is just a simple string replacement (in-situ extension), while typedef is not in-situ extension, and its new name has a certain encapsulation, so that the newly named identifier has the ability to easily define variables. See the third line of the first code:
Typedef (int *) pint;
And the following line:
# Define pint2 int *
____ Have the same effect? Actually different! In practice, see the difference: pint a, B; has the same effect as int * A; int * B; indicates that two integer pointer variables are defined. Pint2 a, B; has the same effect as int * a, B; indicates defining an integer pointer variable A and an integer variable B.
____ Note: there is another line at the end of the two; the difference between the two