A summary of usage of typedef

Source: Internet
Author: User

Summary of usage of typedef (for reference) Original Blog address: http://www.cnblogs.com/csyisong/archive/2009/01/09/1372363.html

Regardless of the C or C + + code, typedef is not uncommon word, of course, the frequency is higher or in C code. typedef and # define are somewhat similar, but more is different, especially in some complex usage, it is completely different, read some of the online and C + + learners of the blog, which has a summary of the typedef is very good, because the summary is very good, I will not add modified reference come over , the following is the referenced content (the red part is what I wrote myself).

Use one:

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;

The following are possible:

typedef char* PCHAR;

PCHAR PA, PB;

This usage is very useful, especially char* PA, PB definition, beginners often think is defined as two-character pointer, actually not, and typedef char* PCHAR will not appear such problems, reduce the occurrence of errors.

Use two:
Used in the old C code to help the struct. In the previous code, when declaring a struct new object, you had to bring a struct with the form: 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 a large number of use

, perhaps, in C + +, this use of TypeDef is not very large, but it is understood, to master the previous old generation

Code is still helpful, after all, we may encounter code that was left behind in the earlier years of 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 one, let it represent the highest precision type:

typedef long double REAL;

On platform two that does not support long double, replace the following:

typedef double REAL;

On three platforms that are not supported by double, replace 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 technique is widely used by the standard library, such as size_t. Also, because typedef defines a new alias for a type, not a simple string substitution, it is more robust than a macro.
this advantage in the process of writing code, we can reduce a lot of code!

Use four:

Define a new, simple alias for a complex claim. The method is: in the original declaration, gradually replaced by an alias

Make a complex declaration, so loop, save the part with the variable name to the last substitution, get the original declaration of the most simplified

Version. Example:

Original declaration: Void (*b[10]) (void (*) ());

For "Void (*b[10]) (void (*) ())," The function description is shown in the following blog:

Http://www.cnblogs.com/leon3000/archive/2007/11/15/2037989.html

The variable name is B, replace the right part in parentheses, and Pfunparam as alias one:

typedef void (*pfunparam) ();

Replace the left variable B,pfunx with the alias two:

typedef void (*PFUNX) (Pfunparam);

The most streamlined version of the original statement:

Pfunx B[10];

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

Variable named E, replace the left part first, Pfuny as alias one:

typedef double (*pfuny) ();

Replace the right variable E,pfunparamy with the alias two

typedef PFUNY (*pfunparamy) [9];

The most streamlined version of the original statement:

Pfunparamy e;

Understanding the right-hand rule that is available for complex declarations: from the variable name, to the right, then to the left, to the parentheses

Turn the direction of reading, in parentheses after the analysis of the parentheses, or the first right after the left order, so loop, straight

To the end of the entire statement analysis. Example:

Int (*func) (int *p);

First find the variable name func, there is a pair of parentheses outside, and the left is an * number, indicating that func is a pointer

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

Func is a pointer to a function such as a function pointer, which has a int* type of formal parameter, and the return value

The type is int.

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

The right side of the Func is a [] operator, stating that Func is an array with 5 elements;

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

Priority is higher than *, and Func is combined with [] first. 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 a return value of type int.

This usage is more complex, the frequency is also many, often in the see this usage but can not understand, I believe that the above explanation can be helpful.

The above is the reference section, the following is my understanding part * * * *

Examples of Use:

1. Comparison 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 = SS;

A (' A ', ' B ');

A = BB;

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

2. Comparison 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

Here is the reference section * * * *

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

typedef and #define的区别:

Case one:

In general, typedef are better than # define, especially where pointers are available. 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 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 substitution and the typedef is a

A new name from a type.

Case TWO:

The compiler will report an error in the following code, 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 reminds us once again: TypeDef and # define are different, it is not simple

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

The const long x is essentially no different, it is a read-only restriction on the variable, except that the variable P2 the data class

Type is what we define ourselves rather than the intrinsic type of the system. Therefore, the meaning of the const PSTR P2 is: limited number

A variable of type char * P2 is read-only and therefore p2++ error. Although the author has explained here very clearly, but I am still confused in this place, I really hope that a master can help to point out, especially this sentence "Only here the variable P2 data type is our own definition rather than the system intrinsic type", Is it possible for a system-defined type to be able to perform a change operation without a const modifier in front of its defined type?

A summary of usage of typedef

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.