Deep analysis of four uses of typedef in C language _c language

Source: Internet
Author: User

Use one:
Define a type of alias, 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; This majority does not conform to our intent, it only declares a pointer to a character variable, and a character variable;
The following works:
typedef char* PCHAR; General in uppercase
Pchar PA, PB; feasible, at the same time declared two pointers to character variables
Although:
Char *pa, *PB; Also feasible, but relatively not in the form of TypeDef intuitive, especially in the need for a large number of pointers, the way the typedef more convenient.

Use two:
Used in the old C code (the specific old did not check), to help struct. In the previous code, when declaring a struct new object, you must take the struct, which is the form: struct the name of the structure name object, such as:

Copy Code code as follows:

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

In C + +, you can write directly: a struct name Object name, which is:
TagPOINT1 P1;
Someone who thinks it's too much trouble to write a struct too often, then invents:
Copy Code code as follows:

typedef struct TAGPOINT
{
int x;
int y;
}point;
Point P1; This is less than the original way to write a struct, more convenient, especially in the mass use of the time

Perhaps, in C + +, the use of a typedef is not very large, but understanding it is useful for mastering old code, after all, we may encounter earlier years of code left over in the project.

Use three:
Use typedef to define platform-independent types.
For example, to define a floating-point type called real, on the target platform, let it represent the highest precision type:
typedef long double Real;
On Platform II, which does not support long double, replace the following with:
typedef double REAL;
On a platform not supported by double, replace the following:
typedef float REAL;
In other words, when cross-platform, just change the typedef itself on the line, do not make any changes to other source code.
The standard library uses this technique extensively, such as size_t.
In addition, because TypeDef is a new alias that defines a type and is not a simple string replacement, it is more robust than a macro (although the use of macros can sometimes do the above).

Use four:
Define a new simple alias for a complex declaration. A simple way to create a type alias for a complex variable is simply to replace the variable name with the type name in the traditional variable declaration expression, and then add the keyword typedef to the beginning of the statement. The principle is to gradually replace some complex declarations with aliases in the original declaration, so that the loop takes the part with the variable name to the last replacement and gets the simplest version of the original declaration. Example:

1. Original declaration: Int * (*A[5]) (int, char*);
Variable named a, replace a directly with a new alias Pfun:
typedef int * (*PFUN) (int, char*);
The most simplified version of the original declaration:
Pfun A[5];

2. Original declaration: void (*b[10]) (void (*) ());
Variable name B, first replace the right part of the bracket, Pfunparam for alias one:
typedef void (*pfunparam) ();
Replace the left variable B,pfunx to alias two:
typedef void (*PFUNX) (Pfunparam);

The most simplified version of the original declaration:
Pfunx B[10];
Understand the "right left" rule available for complex declarations: from the variable name look, first to the right, and then to the left, a round of parentheses to turn the reading direction; After parsing in parentheses, jump out of parentheses, or in the order of the first right and left, so cycle until the entire statement is analyzed. Example:
Int (*func) (int *p);

First find the variable name func, there is a pair of parentheses outside, and a * number on the left, which means that func is a pointer, then jumps out of the parentheses, looks to the right, and then encounters parentheses, which means that (*FUNC) is a function, so func is a pointer to such a function, the function pointer, Such functions have formal parameters of the int* type, and the return value type is int.
Int (*func[5]) (int *);

Func to the right is an [] operator, description func is an array of 5 elements; func has a * on the left, indicating that the Func element is a pointer (note that the * is not cosmetic func, but is decorated func[5] because [] operator precedence is higher than *, Func first with [] Combined). Jump out of this bracket, look to the right, and also encounter parentheses, stating that the element of the Func array is a pointer to a function type, which points to a function that has a int* type parameter, and the return value type is int.

Related Article

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.