Specific detailed differences between typedef and define

Source: Internet
Author: User
Tags define definition

1) #define是预处理指令, in the compilation preprocessing simple substitution, does not make the correctness check, does not have the meaning whether correctly still brings in, only when compiles the already expanded source program only then discovers the possible error and error. For example:
#define PI 3.1415926
In the program: Area=pi*r*r will be replaced with 3.1415926*r*r
If you write the number 9 in the # define statement as the letter G preprocessing is also brought in.

2) A typedef is processed at compile time. It gives an alias to an already existing type within its scope, but you cannot use the typedef specifier inside a function definition.

3) typedef int * INT_PTR;
And
#define INT_PTR int *
The function is to use INT_PTR to represent the int *, but the two are different, as mentioned earlier, a simple substitution #define在预处理, whereas a typedef is not a simple substitution, but rather a type declared as a method of defining a variable. That is

Refer to (Xzgyb (Damour))
#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, B is a pointer to int, and TypeDef introduces a new mnemonic for int*.


This also explains why the following ideas are set up
Qunkangli (Maintenance cost proportional to the square of Programmer's creativity)
typedef int * PINT;
#define PINT int *

So:
Const pint p;//p cannot be changed, but P points can be changed
The const PINT p;//p can be changed, but the content that P points to cannot be changed.

Pint is a pointer-type const pint p is a pointer to the lock p cannot be changed
The const PINT p is a const int * p Lock that is the object referred to by the pointer p.

3) Perhaps you have noticed that # define is not a statement do not add a semicolon at the end of the line, or a semicolon is replaced.

Turn to another article

I. Usage of typedef

A typedef is commonly used to define an identifier and an alias for a keyword in the C + + language, which is part of the language compilation process, but it does not actually allocate memory space, such as:

typedef int INT;
typedef int ARRAY[10];
typedef (INT*) PINT;

typedef can enhance the readability of the program, as well as the flexibility of identifiers, but it also has a "non-intuitive" and other shortcomings.

Second, #define的用法

#define为一宏定义语句, it is commonly used to define constants (including parametric and parametric), and to implement macros that are "seemingly benign, behind a long string", which is not itself in the process of compiling, but before (the pre-processing process) is completed. However, it is also difficult to identify potential errors and other code maintenance problems, and its examples are as follows:

#define INT int#define TRUE 1#define Add (b) ((a) + (b)), #define LOOP_10 for (int i=0; i< ; 10; i++)

In article 1 of effective C + + of Scott Meyer, there is an analysis of the drawbacks of the # define statement, as well as a good alternative, as you can see.

Three, #typedef与 the difference between # define

From the above concepts can also be basically clear, TypeDef is only to increase the readability of the identifier for the new name (just an alias), and # define was originally in C in order to define constants, to the C++,const, enum, The advent of inline makes it a tool for aliases as well. Sometimes it is very easy to figure out with the typedef which good, such as #define INT int such statements, with a typedef can be done, which good? I contend with TypeDef, because this statement is illegal in many of the earlier C compilers, but today's compilers are expanded. To be as compatible as possible, it is common to follow a # define definition of "readable" constants and the tasks of some macro statements, whereas a typedef is often used to define keywords, lengthy types of aliases.

A macro definition is simply a string substitution (in-place extension), whereas a typedef is not an in-place extension, and its new name has a certain encapsulation so that the newly named identifier has the ability to define variables more easily. Take a look at the third line of the first big point of the code above:

typedef (int*) PINT; and the following line:#define PINT2 int*

Same effect? It's different! See the difference in practice: PINT A, B, the effect with int *a; An int *b that defines two integer pointer variables. And pINT2 A, B; the effect is the same as int *a, b. The expression defines an integer pointer variable A and integer variable B.

Note: There is also a line at the end of the number of the difference Oh!

Specific detailed 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.