C & C + +, and # define typedef
First, the use of typedef
___ In the C / C + + language, used to define a typedef identifiers and keywords alias, it is part of the process of language compiler, but it does not actually allocate memory space, as examples:
Typedef int INT;
Typedef int ARRAY [10];
Typedef (int *) pINT;
____typedef Can enhance the readability of procedures, as well as the flexibility of identifier, but it also has "non-intuitive" and other shortcomings.
Second, the use of # define
____# Define a macro definitions for the phrase, it is usually used to define constants (including non-parametric and parametric band), as well as to the realization of those "superficial seems benign, behind a long list of" Acer, it does not in the course of compiler , but before this (preconditioning process) has been completed, but it is difficult to identify potential errors and other code maintenance, as its examples:
# Define INT int
1 # define TRUE
# Define Add (a, b) ((a) + (b));
# Define Loop_10 for (int i = 0; i <10; i + +)
___ Scott Meyer in the Effective C + + book in the terms of a # define statement on the defects of the analysis, as well as better alternative, we can see.
Third, typedef and # define the difference
___ From the above basic concept can be clearly typedef only to increase readability and another identifier for a new name (merely individual), and the original # define in C is to define a constant, C + +, const, enum, and the emergence of inline it has gradually become a tool from Alias. Sometimes not very easy to engage with the typedef both in the end by which the good, such as # define INT int such statements, to be completed by the same typedef, which is good use? I advocate the use of typedef because in the early days many of the C compiler in this sentence is illegal, but the current compiler done expansion. To the extent possible, compatible and generally follow the definition # define "machine-readable"-as well as some constants statement of the mandate, typedef is commonly used to define keywords, the type of lengthy alias.
____-Definition simply string substitution (in situ expansion), while typedef is not in situ, and its new name of a certain package, which in the name of the new identifier is more easily defined variables functions. See the first point above the third line of code:
Typedef (int *) pINT;
Below this line, as well as:
# Define pINT2 int *
___ Same effect? In fact different! See differences in practice: pINT a, b; with the effect of int * a; int * b; said that the definition of the two integer pointer variable. The pINT2 a, b; int * with the effect of a, b; said that the definition of a variable an integer pointer and integer variables b.
___ Note: Taken there is a end-of-line; of the difference!








0 Comments to “C & C + +, and # define typedef”
No Comments. Send your comment.
Leave a Reply
You must be logged in to post a comment.