Specific detailed differences between typedef and define

Source: Internet
Author: User
Tags function definition
1) #define是preprocessing Directives, which is performed when the preprocessing is compiledsimple substitution, no correctness check, whether the meaning is correct or not, only when compiling the source program has been expanded to find possible errors and error.

For example: #define PI 3.1415926 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; As with #define INT_PTR int *, the function is to use INT_PTR to represent int *, but they 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 #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 view is set up//qunkangli (maintenance cost proportional to the square of Programmer's Creativity) typedef int * PINT;

#define PINT int * Then: the const PINT p;//p cannot be changed, but the contents of P point can change the const PINT p;//p can be changed, but the contents of P point cannot be changed.

Pint is a pointer-type const pint P is to lock the pointer to P cannot be changed and the const pint p is a const int * p Lock is the object that the pointer p refers to.


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. Reprint Http://zhidao.baidu.com/link?url=
 Jr1uch9jjksz1mzioxnvk3l53u66lqey6wdztcvxaxi6mbpkg7owrv6hg6iwihzuqeyoawxim8rr1sgd_c46rq

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.