Difference between typedef and # define

Source: Internet
Author: User

 

Difference between typedef and # define

 

The benefits of renaming constants using macro definitions: The program is easy to implement and easy to maintain and modify; 1) # define is a pre-processing command that performs simple replacement during compilation and preprocessing without checking the correctness, if the meaning is correct or not, possible errors are discovered and an error is 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;

// Refer to (xzgyb (Lao Damo ))
# 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.

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 (because pint is a pointer, it is equivalent to a const pointer, so the pointer cannot be changed, but the content can be changed)
Const pint P; // P can be changed, but the content pointed to by P cannot be changed. (Because # define is a simple replacement, so it is equivalent to const int * P, which is a pointer to a constant)

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) You may have noticed that # define is not a statement. Do not add points at the end of the line. Otherwise, a semicolon is replaced.

Another article

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, such:

Typedef int;
Typedef int array [10];
Typedef (int *) pint;

Typedef enhances program readability and the flexibility of identifiers, but it also has 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 to implement macros that are "superficial and kind, long strings behind them, it is not carried out in the compilation process, but completed before (preprocessing), but it is difficult to find potential errors and other code maintenance problems, 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 analyzed the disadvantages of # define statements and good alternatives. For more information, see.

Iii. Differences between # typedef and # define

From the above concepts, we can also understand that typedef is just a new name (only an alias) for the identifier to increase readability, and # define is originally used to define constants in C, the emergence of C ++, const, Enum, and inline has gradually become 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 is invalid, but today's compilers
It has been 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 names.

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 new name identifier has a more convenient function to define variables. See the third line of the first code:

Typedef (int *) pint;And the following line:# Define pint2 int *

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.