C-language typedef detailed

Source: Internet
Author: User

In C or C + + code, typedef uses a lot. typedef and # define are somewhat similar, in fact, different.

Basic definition: A typedef is a C keyword that defines a new name for a data type, where the data type includes the base data type (Int,char), and also the custom data type (struct).

(1) The difference from # define

A typedef is a bit like a # define macro whose actual type is substituted for synonymous words. The difference is that the TypeDef is interpreted at compile time, so the compiler is able to cope with the text substitution beyond the preprocessor's capabilities.

(2) Reduce errors

Defines an alias for a type, not just a simple macro substitution. Can be used as multiple objects that declare a pointer type at the same time. Like what:

char* PA,PB;

The majority does not conform to our intent, it only declares a pointer to a character variable and a character variable. You can modify it in the following ways:

typedef char* PCHAR;PCHAR PA,PB;
This method is very useful, especially char* PA,PB, the definition of beginners often think is defined as two-character pointer, is not, and with typedef char* PCHAR will not appear this problem, reduce the occurrence of errors.

(3) intuitive and simple

Used in the old C code, in conjunction with the struct. In the previous code, when declaring a struct new object, you had to bring a struct with the form: struct struct Body name Object name. Such as:

struct TAGPOINT1 {    int x;    int y;}; struct tagPOINT1 p1;

If the typedef code is used as follows:

typedef struct TAGPOINT {    int x;    int y;} Point; Point p1;//can do this, and struct tagpoint p2;//can do the same;

Or:

typedef struct {    int x;    int y;} Point; Point P1;

Point P1; This is less than the original way to write a struct, more convenient, especially in a lot of use.

(4) Platform independence

Use typedef to define platform-independent types. For example, a real floating-point type can be defined, and the highest precision can be obtained on the target machine.

typedef long double REAL;
On a machine that does not support a long double, it can be modified as follows:

typedef double REAL;
If double is not supported, modify the following:

typedef float REAL;

That is, when cross-platform, just change the typedef itself, do not make any changes to other source code.

This article refers to: http://blog.csdn.net/wangqiulin123456/article/details/8284939.

C-language typedef detailed

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.