Summary of typedef usage

Source: Internet
Author: User

Whether it is C or C ++CodeThe word typedef is widely used. Of course, the frequent occurrence is still in the C code. Typedef is similar to # define, but it is more different, especially in some complicated usage, I read some blogs of C/C ++ learners on the Internet. One of the blogs about typedef is still very good. Because of the excellent summary, I will not add any modified references, the following is the reference content (The red part is my own content.).

Purpose 1:

Define a type of Alias, not just a simple macro replacement. It can be used as multiple objects that declare the pointer type at the same time. For example:

Char * pA, PB ;//Most of these do not conform to our intention. It only declares a pointer to a character variable,

//And a character variable;

The following are feasible:

Typedef char * pchar;

Pchar Pa, PB;

This usage is very useful, especially the definition of char * pa and Pb. Beginners often think that two character-type pointers are defined. Actually, this is not the case. Using typedef char * pchar won't happen, reduces errors.

Purpose 2:
It is used in the old C code to help struct. In previous Code, when declaring a new struct objectStruct, in the form of: struct structure name object name, such:

Struct tagpoint1

{
Int X;

Int y;
};

Struct tagpoint1 P1;

In C ++, you can directly write: Structure name object name, that is, tagpoint1 P1;

Typedef struct tagpoint
{
Int X;

Int y;
} Point;

Point P1; // in this way, less struct is written than the original method, which is easier to use, especially when a large number

Mr. Maybe, in C ++, this type of purpose 2 of typedef is not very big, but I understand it and grasp the old generation

Code is still helpful. After all, we may encounter code left over from earlier years in the project.

Purpose 3:

Use typedef to define platform-independent types.

For example, define a floating point type called real. On the target platform 1, make it the highest precision type:

Typedef long double real;

On Platform 2 that does not support long double, change:

Typedef double real;

On Platform 3 that is not supported by double, change:

Typedef float real;

That is to say, when using a cross-platform system, you only need to change the typedef itself, without any modifications to other source codes.

This technique is widely used in the standard library, such as size_t. In addition, because typedef defines a new type of Alias, rather than a simple string replacement, it is more robust than a macro.
This advantage can reduce the amount of Code while writing code!

Purpose 4:

Reprinted from: http://www.cnblogs.com/csyisong/archive/2009/01/09/1372363.html

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.