Specific specific differences in 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 to carry in, only has in the compilation has been expanded the source program only then discovers the possible error and errors. Like what:
#define PI 3.1415926
In the program: Area=pi*r*r will be replaced with 3.1415926*r*r
Suppose you write the number 9 in the # define statement as the letter G preprocessing is also carried 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 the program Ape'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 a line, or a semicolon is replaced.

Turn to another article

I. How to use typedef

A typedef is often used to define an identifier and an alias for 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 often used to define constants (including non-reference and band), and to implement those "seemingly benign, behind a long string" of macros, which itself is not in the process of compiling, but before this (the pre-processing process) is finished, However, it is also difficult to identify potential errors and other code maintenance problems, which can be seen in instances like:

#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, which you can take a look at.

Three, #typedef与 the difference of # define

From the above concepts can also be basically clear, TypeDef is simply to add readability and the identifier for the new name (just the individual name), and # define was originally in C to define constants, to the C++,const, enum, The advent of inline makes it a tool for aliases as well. Sometimes very easy to figure out with the typedef which good, such as #define INT int such statement, with a typedef can complete, with which good? I contend that with TypeDef, because this statement is illegal in many of the earlier C compilers, it is only the compiler that is now expanding. 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 TypeDef is often used to define keyword, lengthy types of aliases.

A macro definition is simply a simple 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*

The 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: The difference between the other lines;


Specific specific differences in 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.