typedef usage and traps

A, typedef usage

1. Use typedef to declare a new type name to replace the existing type name, which is to type aliases. such as

. 1 typedef float REAL; // used to represent a float REAL 
2 REAL a; // define a variable of type REAL, equivalent to a float, i.e., a definition of a variable of type float

This usage is often used as a definition of the type of platform-independent, cross-platform code easily.

For example, the definition of REAL type for the target platform's most accurate type.

1> is defined on a supported platform for long double:

typedef long double REAL; 

Note: long double increase of C99 type, ANSI C standard specifies double variables are stored as IEEE 64-bit (8-byte) floating-point value, but does not specify the exact long double precision, but a predetermined number of long double precision in the double precision. So there may be different for different platforms long double implementations, some 8 bytes, some 10 bytes, 12 bytes or 16 bytes some. For specific circumstances compiler may be learned by sizeof (long double).

2> instead on the platform does not support long double

typedef double REAL; 

3> read on double unsupported platform 

typedef float REAL; 

Therefore, when the cross-platform, as long as the change the next typedef itself on the line, do not make any other changes to the source code.

2. Custom data structure (e.g., struct, union) defined simple type and easy to remember names.

such as:

struct data {
     int month The;
     int Day;
     int year; 
}; // definition of a data structure of

When the structure is declared as variables, as follows

struct data birthday;

 

New types may be defined using typedef, in place of the above structure type

typedef struct data {
     int month The;
     int Day;
     int year; 
} DATA; // definition of a data structure of the 

DATA Birthday; // define a variable of type DATA birthday, struct data type is equivalent to the above

Note: This method is not recommended and prohibited in some code specification, because the new type of hide specific type, look at the code is not conducive to the actual type of people intuitively understand.

3. Use typedef to declare a new type

1> can be used to simplify the array variable defined

typedef int ARR [ 10 ]; // define an int type array ARR containing 10 elements 
ARR arr_a, arr_b; // defines two variables int array contains 10 elements arr_a, arr_b

2> defined simplified pointer variable

typedef int * the INT_P 
the INT_P p1, p2; // define two pointer variables int p1, p2

Note that the following definition of error-prone, so the programming specification is prohibited once declare multiple variables

int * P1, p2; // P1 is of type int *, p2 is an int
. 1 typedef char * STRING;
 2 STRINT P; // P is a pointer to the string variable 
. 3 STRINT s [ 10 ]; // s is an array of pointers, i.e., s is an array of 10 elements comprising each element is a string pointer 
. 4  
. 5 typedef int (* the pOINTER) (); // declare POINTTER a pointer to the function type, the function returns a value of type int 
. 6 the pOINTER P1, P2; // P1, P2 are a function pointer variable;

4. complex simple declaration defines a new alias.

int * (A * [ . 5 ]) ( int , char *); // variable named a, directly replaced with a new alias pFun A 

typedef int * (pFun *) ( int , char * ); 
pFun A [ . 5 ] ; // define a containing 5 array of function pointers
void (* B [ 10 ]) ( void (*) ()); // the original statement, b is an array of pointers containing function elements 10, which parameters are no parameters function pointer 

typedef void (* pFunParam) () ; // function pointer declared with no parameters is the type pFunParam 
typedef void (* pFunx) (pFunParam); // declare function pointer pFunx 
pFunx b [ 10 ]; // array of function pointers b
. 1 doube (*) () (* e) [ . 9 ]; // variable e is a pointer that points to an array, the array comprising nine elements, each element is a function pointer, the function returns the pointer value double type 
2  simplified wording:
 . 3 typedef double (* pFuny) (); // declare a function pointer alias pFuny, return type is double 
. 4 typedef pFuny (* pFunParamy) [ . 9 ]; // declare a pointer 
. 5 pFunParamy E; / / declared type of the variable e is pFunParamy

Understand "right-left rule" complicated claims is available:

Looks from the variable name, first right, then left, then turned around and hit a parenthesis to read; parentheses analysis done to come out brackets, or by the rear left to right order, and so on, until the entire statement After analyzing . For example:
int (* func) (the p-int *);
First find the variable name func, outside a pair of parentheses, and the left is an asterisk, indicating that func is a pointer; then out of this parenthesis, look to the right, and parentheses encountered, indicating (* func) is a function, it is a pointer to such func function, i.e. a function pointer, the function having such a parameter of type int *, return type is int.
int (* func [. 5]) (int *);
func right is a [] operator, described func having an array of five elements; left func has a *, described func element pointer (note that * not modify func, but modified func [5], because [] operator * higher priority than talk, FUNC [] binding). Out of the bracket, see the right, parentheses encountered, indicating that the element is a function func array type pointer, which points to a function having a parameter of type int *, return type to int.

You may remember the two modes:
type (*) (....) function pointer 
type (*) [] array pointer

Two, typedef trap

1.typedef is a new type of statement is not a simple string replacement, unlike #define

E.g:

 

1 typedef char* STRING;
2 int strcmp(const STRING,const STRING);

 

In the above code, "const STRING" whether or not the equivalent of "const char *" it?

The answer is no, the reason is very simple, typedef is used to declare a new type, which is different from the macro, not a simple string replacement. STRING has a type, const modify the type is constant. Thus, "const STRING" in a given const STRING constant properties, i.e. the whole of the pointer itself constant, i.e. forming a constant pointer "char * const (a constant pointer to char)."

I.e., it is actually equivalent to "char * const", instead of "const char * (pointing to const char)." Of course, to get equivalent const char * const PCHAR also easy, as shown in the following code:

 

 

typedef const char* PCHAR;
int strcmp(PCHAR, PCHAR);

 

Therefore, no matter what time, as long as the pointer typedef statement, then it should add a const to the resulting typedef name, so that the pointer itself is constant.

2.typedef and storage class (storage class specifier)

typedef keyword is syntactically a storage class (such as auto, extern, mutable, static, register, etc. as), storage characteristics although it does not really affect the object.

For example, the following code will compile error:

1 typedef static int INT_STATIC;

 

 

 

reference:

https://www.cnblogs.com/a-s-m/p/10995722.html

http://c.biancheng.net/view/298.html

Guess you like

Origin www.cnblogs.com/bluenightbreeze/p/11444457.html