A summary of the usage of typedef

Source: Internet
Author: User

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


No matter C or C + + code, the word typedef is not uncommon, of course, the frequency is high or in C code. The typedef are somewhat similar to the #define, but more is different, especially in some complex usage, it is completely different, look at some of the Internet C + + of the learner's blog, which has a summary of the typedef is very good, because the summary is very good, I will not modify the quote over , here are the references (the red part is what I write myself).

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;

Pchar PA, PB;

This usage is very useful, especially the definition of char* PA, Pb, beginners often think that is the definition of two character pointer, is not, and with a typedef char* PCHAR will not appear such problems, reduce the occurrence of errors.

Use two:
Used in the old C code to help struct. In previous code, when declaring struct a new object, you must take a struct, in the form of a struct struct name object name, such as:

struct TAGPOINT1

{
int x;

int y;
};

struct tagPOINT1 p1;

In C + +, you can write directly: Structure name Object name, namely: TagPOINT1 P1;

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

Wait, perhaps, in C + +, this use of TypeDef is not very large, but understand it, to grasp the previous generation

Code is helpful, after all, we are likely to experience a legacy of earlier years 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. Also, because TypeDef is a new alias that defines a type and is not a simple string replacement, it is more robust than a macro.
This advantage in the process of writing code we can reduce a lot of code OH.

Use four:

Define a new simple alias for a complex declaration. In the original declaration, replace the alias with a

A complex declaration, so loop, to leave the variable name of the part of the last replacement, get the original declaration of the most simplified

Version. Example:

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

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

Variable name e, replace the left part first, Pfuny is alias one:

typedef double (*pfuny) ();

Replace the right-hand variable E,pfunparamy with the alias two.

typedef PFUNY (*pfunparamy) [9];

The most simplified version of the original declaration:

Pfunparamy e;

Understand the "right left" rule available for complex declarations: Look at the variable name, first to the right, then to the left, to the parentheses.

To switch the direction of reading, after the analysis in parentheses, jump out of parentheses, or the first right after the left order, so that the cycle, straight

Complete the analysis of the entire declaration. 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

And then jump out of this parenthesis, first look to the right, and then encounter parentheses, which means (*func) is a function, so

Func is a pointer to such a function, a function pointer, that has a int* type parameter, and returns a value

Type is int.

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

Func to the right is an [] operator, which shows that Func is an array of 5 elements; func has a * on the left, indicating

The Func element is the pointer (note that the * is not a cosmetic func, but a modifier func[5], because the [] operator

Priority ratio * High, func first with [] combined). Jump out of this bracket, look to the right, and encounter parentheses, indicating the number of Func

The element of a group 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.

This usage is more complex, the frequency also appears many, often see such usage but can't understand, believe above explanation can help.

For the reference section, the following is part of my understanding of * * *

Use examples:

1. Compare One:

#include <iostream>

using namespace Std;

typedef int (*a) (char, char);

int SS (char A, char b)
{
cout<< "Function 1" <<endl;

cout<<a<<endl;

cout<<b<<endl;

return 0;
}

int BB (char A, char b)
{

cout<< "Function 2" <<endl;

cout<<b<<endl;

cout<<a<<endl;

return 0;

}

void Main ()
{

A A;

A = SS;

A (' A ', ' B ');

A = BB;

A (' A ', ' B ');
}

2. Compare two:

typedef int (A) (char, char);

void Main ()
{

A *a;

A = SS;

A (' A ', ' B ');

A = BB;

A (' A ', ' B ');
}

The results of both programs are the same:

Function 1

A

B

Function 2

B

A

The following is a reference section * * *

Reference from: http://blog.hc360.com/portal/personShowArticle.do?articleId=57527

typedef and #define的区别:

Case one:

Generally speaking, a typedef is better than a #define, especially in a pointer situation. Take a look at the example:

typedef char *PSTR1;

#define PSTR2 char *;

PSTR1 S1, S2;

PSTR2 S3, S4;
In the above variable definition, s1, S2, S3 are all defined as char *, and S4 is defined as char, not our

The underlying reason for the expected pointer variable is that #define is simply a string replacement and the typedef is a

Type to start a new name.

Case TWO:

The compiler in the following code will report an error, do you know which statement is wrong?

typedef char * PSTR;

Char string[4] = "ABC";

const char *P1 = string;

Const PSTR P2 = string;

p1++;

p2++;

It was p2++ that made a mistake. This question once again reminds us that the TypeDef and #define are different, it is not simple

Text replacement. The const PSTR P2 in the above code is not equal to the const char * p2. Const PSTR P2 and

The const long x has no difference in nature and is a read-only restriction on a variable, except that the data class of the variable P2 here

The type is defined by ourselves rather than by the intrinsic type of the system. Therefore, the meaning of the const PSTR P2 is: Limit the number

A variable of type char * P2 is read-only and therefore p2++ error. Although the author has explained it very clearly here, but I am still confused in this place, I really hope that a master can help guide, especially this sentence "Only here variable P2 data type is our own definition rather than the system inherent type", do you define the type before the const modified , you cannot perform the change operation, but the system-defined type is OK.

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.