1) # define is a pre-processing command. It is a simple replacement during compilation and pre-processing, and does not check the correctness. It does not mean that it is correct or not, possible errors are detected and reported only when the expanded source program is compiled. For example:
# Define PI 3.1415926
In the program: Area = pI * r will be replaced with 3.1415926 * r * R
If you write the number 9 in the # define statement as a letter g, the preprocessing will also be carried in.
2) typedef is processed during compilation. It gives an existing type an alias 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 *
Int_ptr is used to represent int *, but the two are different. As mentioned above, # define is replaced simply during preprocessing, while typedef is not a simple replacement, instead, declare a type as if the variable is defined. That is to say;
# Define int_ptr int *
Int_ptr a, B; // equivalent to int * a, B; just a simple macro replacement
Typedef int * int_ptr;
Int_ptr a, B; // A and B all point to the int pointer. If typedef is int *, a new mnemonic is introduced.
Notes for using typedef.
Typedef char * string;
Const string S;
What is the above definition?
Const char * s; // s is a pointer to const char (the content pointed to by the pointer is read-only)
Or
Char * const s; // s is the const pointer to Char (The position of the pointer itself cannot be changed)
The correct answer is the latter. Because after typedef defines the string, the const object is the string object and the string itself
Is a pointer, so const string means that the value of string is a constant. Finally, S is interpreted as pointing to the const of char.
Pointer.
======================================
This also explains why the following points are true:
// Qunkangli (the maintenance cost is directly proportional to the programmer's creativity)
Typedef int * pint;
# Define pint int *
So:
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 used to lock the pointer. P cannot be changed.
Const pint P is the object indicated by pointer p.
3) # define is not a statement. Do not add points at the end of the line. Otherwise, a semicolon is replaced.
# Define pin1 int * typedef (int *) pin2; the two are similar in form, but they are different in actual use. The following uses pin1, ndfnl + W for "@, 3cw M tutorial network pin2 to illustrate the differences between variables: pin1 A, B; after the macro replacement, it becomes int * a, B; indicates that A is a pointer variable pointing to an integer, and B is an integer variable. However, pin2 a, B; indicates that both A and B are pointer variables pointing to integer types. Because pin2 is a type specifier.