Usage of typedef and its difference with # define

Source: Internet
Author: User

Typedef

Usage

 

 

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 them 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;

Purpose 2:

Used in the old cCodeTo help struct. In the previous Code, when declaring a new struct object, you must carry the struct, that is, the form is: struct structure name object name, such:
Struct tagpoint1 <br/>{< br/> int X; <br/> int y; <br/>}; <br/> struct tagpoint1 P1;

In C ++, you can directly write: Structure name object name, that is, tagpoint1 P1;
<Br/> typedef struct tagpoint <br/>{< br/> int X; <br/> int y; <br/>}point; <br/> Point P1; // In this way, a struct is written less than the original method, which is easier to use, especially when it is used in a large amount.

Maybe, in C ++, typedef is not very useful, but understanding it is helpful for mastering the old code, after all, we may encounter code that was left behind in 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.

 

Purpose 4:

 

Define a new simple alias for complex statements. The method is: gradually replace a part of the complex declaration with an alias in the original declaration. In this loop, leave the part with the variable name to the final replacement, and the most simplified version of the original declaration is obtained. Example:

Original Declaration: void (* B [10]) (void (*)());

The variable name is B. Replace pfunparam with alias 1 in the brackets on the right:

Typedef void (* pfunparam )();

Replace B with the variable on the left, and pfunx with alias 2:

Typedef void (* pfunx) (pfunparam );

The most simplified version of the original statement:

Pfunx B [10];

Original statement: Doube (*) (* E) [9];

The variable name is E. Replace the left part with pfuny as Alias 1:

Typedef double (* pfuny )();

Replace the variable E on the right, and pfunparamy is alias 2.

Typedef pfuny (* pfunparamy) [9];

The most simplified version of the original statement:

Pfunparamy E;

Understand the "right-left rule" available for complex statements: Starting from the variable name, you must first go to the right and then to the left. When you encounter a parentheses, you can adjust the reading direction. After analyzing the parentheses, you can jump out of the brackets, it is still in the order of the first right and then the left, until the entire declaration analysis is complete. Example:

INT (* func) (int * P );

First, find the variable name func, and there is a pair of parentheses on the outside, and there is a * sign on the left, which indicates that func is a pointer. Then jump out of the parentheses and look at the right first, this indicates that (* func) is a function, so func is a pointer to this type of function, that is, a function pointer. This type of function has an int * type parameter, and the return value type is int.

INT (* func [5]) (int *);

The right side of func is a [] Operator, indicating that func is an array with five elements; there is a * on the left of func, indicating that the func element is a pointer (note that * is not a modifier of func, instead, modify func [5] Because the [] operator has a higher priority than *, and func is first combined ). Jump out of the brackets and look at the right side. The parentheses indicate that the element of the func array is a pointer of the function type. It points to an int * type parameter and the return value type is int.

 

*****

The above is the reference part, and the following is my understanding part.

*****

 

Example:

 

1. Comparison 1:
<Textarea Cols = "50" rows = "15" name = "code" class = "CPP: nogutter: nocontrols" >#include <iostream> <br/> using namespace STD; <br/> typedef int (* A) (char, char); <br/> int SS (char a, char B) <br/>{ <br/> cout <"function 1" <Endl; <br/> cout <A <Endl; <br/> cout <B <Endl; <br/> return 0; <br/>}< br/> int BB (char a, char B) <br/>{ <br/> cout <"function 2" <Endl; <br/> cout <B <Endl; <br/> cout <A <Endl; <br/> return 0; <br/>}< br/> void main () <br/>{< br/> A; <br/> A = SS; <br/> A ('A', 'B '); <br/> A = BB; <br/> A ('A', 'B'); <br/>}</textarea>

2. Comparison 2:
Typedef int (A) (char, char); <br/> void main () <br/>{< br/> A * A; <br/> A = SS; <br/> A ('A', 'B'); <br/> A = BB; <br/> A ('A', 'B '); <br/>}< br/> the results of the two <SPAN class = 'wp _ keywordlink '> Programs </span> are the same: <br/> function 1 <br/> A <br/> B <br/> function 2 <br/> B <br/>

* ***** The following is a reference section *****

Differences between typedef and # define:

Case 1:

Generally, typedef is better than # define, especially when there is a pointer. See the example below:
Typedef char * pstr1; <br/> # define pstr2 char *; <br/> pstr1 S1, S2; <br/> pstr2 S3, S4;

In the preceding variable definition, S1, S2, and S3 are both defined as char *, while S4 is defined as char, which is not the expected pointer variable, the root cause is that # define is a simple string replacement, while typedef is a new name for a type.

Case 2:

In the following code, the compiler reports an error. Do you know which statement is wrong?
Typedef char * pstr; <br/> char string [4] = "ABC"; <br/> const char * P1 = string; <br/> const pstr P2 = string; <br/> P1 ++; <br/> P2 ++;

P2 ++ error. Another reminder is that typedef is different from # define, which is not a simple text replacement. In the above Code, const pstr P2 is not equal to const char * P2. There is no difference between const pstr P2 and const long x in nature. They both impose read-only restrictions on variables, except that the data type of the variable P2 is defined by ourselves rather than the inherent type of the system. Therefore, const pstr P2 indicates that the variable P2 with the Data Type limited to char * is read-only, so P2 ++ is incorrect.

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.