Differences between typedef and define

Source: Internet
Author: User
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
Assume that you write the number 9 in the # define statement into the letter G for preprocessing.

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 performs simple replacement during preprocessing, while typedef is not a simple replacement, instead, declare a type as defined in the variable method. That is to say;

// Refer to (xzgyb (Lao Damo ))
# 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 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 square of 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) 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 identifier and the alias of 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 can enhance program readability and the flexibility of identifiers, but it also has some disadvantages such as "non-intuitive.

Ii. # define usage

# Define is a macro definition statement. It is often used to define constants (including non-attention and attention-free values) and macros used to implement "superficial kindness, a long string behind, 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 an analysis on the disadvantages of # define statements and a good alternative method.

Iii. Differences between # typedef and # define

From the above concepts, we can also basically understand that typedef is just to add? For readability, it is a new name (only an alias) for the identifier, and # define is originally used in C to define constants. In C ++, the appearance of const, Enum, and inline gradually makes it an alias tool. Sometimes it's very easy. I don't know which one should be used for typedef, for example, the # define int statement. Which one can be used for the same purpose? I advocate the use of typedef, because this statement was invalid in many early C compilers, it is only the current compiler that 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 keyword 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 *

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 a difference between the end and the end of the other line!


Differences between typedef and define

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.