In C + + code, the typedef uses a lot, especially in C code. typedef and #define Some similar, in fact, is different, especially in some complex usage, read some of the online C/s + + 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 came over, Add an analysis of your own.
Basic Definition:
A typedef is a keyword in the C language that defines a new name for a data type. The data types here include internal data types (Int,char, etc.) and custom data types (struct, etc.). There are two commonly used typedef in programming, one is to give variables a new name that is easy to remember and meaningful, and the other is to simplify some of the more complex type declarations.
Use one: the difference with the #define
A typedef behaves somewhat like a #define macro, replacing the synonym with its actual type. The difference is that the TypeDef is interpreted at compile time, so the compiler is allowed to deal with text substitutions beyond preprocessor capabilities.
use two: Reduce the error
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:
Copy Code code as follows:
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:
Copy Code code as follows:
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 three: intuitive and concise
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:
Copy Code code as follows:
struct TAGPOINT1
{
int x;
int y;
};
struct tagPOINT1 p1;
In C + +, you can write directly: Structure name Object name, namely: TagPOINT1 P1;
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 a large number of use, perhaps, in C + +, the use of the TypeDef two is not very big, but understand it, to grasp the old code is still helpful, After all, we are likely to experience the code left over from the earlier years in the project.
use four: platform independent sex
Use typedef to define platform-independent types.
There is another important use of TypeDef, which is to define machine-independent types, for example, you can define a floating-point type called real, which can achieve the highest precision on the target machine:
Copy Code code as follows:
typedef long double Real;
On a machine that does not support long double, the typedef looks like the following:
Copy Code code as follows:
Also, on a machine that does not have a double, the typedef looks like this:
Copy Code code as follows:
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.
use five: disguise compound type
A typedef can also mask compound types, such as pointers and arrays.
For example, you don't have to repeatedly define an array of 81 character elements as follows:
Copy Code code as follows:
Char line[81];
Char text[81];
Defines a typedef that you can do whenever you want to use an array of the same type and size:
Copy Code code as follows:
The line type now represents an array of characters with 81 elements, using the following method:
Copy Code code as follows:
line text, Secondline;
Getline (text);
Similarly, you can hide the pointer syntax as follows:
Copy Code code as follows:
typedef char * PSTR;
int mystrcmp (PSTR, PSTR);
This will take us to the first typedef trap. The standard function strcmp () has two parameters for the ' const char * ' type. Therefore, it may mislead people to declare mystrcmp () as follows:
Copy Code code as follows:
int mystrcmp (const PSTR, const PSTR);
With the GNU GCC and g++ compilers, there will be warnings, in the order that ' const PSTR ' is interpreted as ' char* const ' (a pointer constant to char), both of which express the same meaning. In order to get the correct type, it should be stated as follows:
Copy Code code as follows:
typedef const CHAR* PSTR;
use six: Code simplification
Code simplification. Define a new simple alias for a complex declaration. The method is to replace a part of the complex declaration with an alias 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:
Original statement:
Copy Code code as follows:
void (*b[10]) (void (*) ());
Variable name B, first replace the right part of the bracket, Pfunparam as the alias
Copy Code code as follows:
typedef void (*pfunparam) ();
Replace the left variable B,pfunx to alias two:
Copy Code code as follows:
typedef void (*PFUNX) (Pfunparam);
The most simplified version of the original declaration:
Copy Code code as follows:
Original statement:
Copy Code code as follows:
Variable name e, replace the left part first, Pfuny is alias one:
Copy Code code as follows:
typedef double (*pfuny) ();
Replace the right-hand variable E,pfunparamy with the alias two.
Copy Code code as follows:
typedef PFUNY (*pfunparamy) [9];
The most simplified version of the original declaration:
Copy Code code as follows:
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:
Copy Code code as follows:
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.
Copy Code code as follows:
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.
Use VII: typedef and Storage-Class keywords (storage class specifier)
This is not a bit surprising, the TypeDef, like Auto,extern,mutable,static, is a storage-class keyword, as is the register. This is not to say that a typedef really affects the storage characteristics of an object; it simply says that the TypeDef declaration looks like a variable declaration of a type such as Static,extern on the composition of the statement. Here's a second trap:
Copy Code code as follows:
typedef register INT Fast_counter; Error
compilation does not pass. The problem is that you can't have multiple storage-class keywords in the declaration. Because the symbol typedef already occupies the location of the storage class keyword, the register (or any other storage class keyword) cannot be used in a typedef declaration.