typedef is a keyword that can rename some data types or redefine some of the new data types. We understand typedef from the following points of view.
1.typedef and normal data type types
typedef int Myint;int A = 10; MYINT B = 20;123123
We rename int to Myint, when Int and Myint are the same, you can define the variable, and the 32-bit platform is 32-bit.
2.TYPEDEF and structure (emphasis)
struct people {int age; char sex;}; 1234512345
The above creates a new data type Strcut people, note that this is a data type and the data type does not account for memory.
struct people p1,p2;11
At this time we defined two variables, p1 and P2, their data type is a struct people, after each of us to define the variable to write the struct people, is not feeling very troublesome, using typedef to rename it is much simpler.
typedef struct people{int age; char sex;} STRPEO; 1234512345
So the struct people and strpeo and struct people have the same effect. After we define the variables directly strpeo p1,p2, is not much simpler.
Let's define a pointer to the struct.
typedef struct PEOPLE
{
int age;
char sex;
}*PSTRPEO;
Although * is close to PSTRPEO, but * is combined with a struct people, that is, the type of PSTRPEO is a struct people *
Strpeo P1; Define struct-body variable p1.age = 20;//Assign value to struct age member pstrpeo P = &p1; Use struct pointers to access the age member printf ("%d\n", p->age); The result is 20 12341234
3.typedef and # define (emphasis)
A typedef is a type rename, equivalent to a data type, and # define is a macro definition that is replaced directly during program preprocessing
typedef int * PMYINT; MYINT A, B; 123123
Equivalent to int* a,int* B, the data type of the two variables is int *
#define PMYINT int *pmyint a,b;123123
* Macro definition is a direct replacement, in this is the substitution of int out pmyint equivalent to int a,b;a is the int type, b is the int type. **
4.typedef and const (emphasis)
Review: const int * p; The variable that the pointer p points to is not becoming
int * const P; The pointer p itself is immutable
typedef int* PINT;CONST PINT p1; PINT Const P2;12341234
according to our usual understanding should be const int* P1; and the variable that the int *const p2;p1 points to is immutable, and the P2 pointer itself is immutable. After code validation we found that either the const PINT P1, or the PINT const p2; The effects are equivalent to int *const, p1 and P2 are immutable. What if we want the variable that the pointer points to to be immutable?
typedef const INT* Cpint; Cpint P1,cpint P2; This way the P1 and P2 themselves are mutable, and the variables pointed to are immutable. 123123
5.typedef and Function pointers
function: INT * TEST (const char *,int);
function Pointer int* (* ptest) (const char *, int)
Every time we define this kind of function, the pointers are written so long that it's troublesome
typedef int * (* PTEST) (const char *, int);
In the future we use this function pointer directly to Ptest P1,P2;
6. Meaning of using typedef
The advantage of using TypeDef is to simplify the writing of types, create platform-independent data types, facilitate code porting, int is 32-bit on 32-bit machines, and the programs we write on 32-bit machines will probably not work on 64-bit platforms, we can redefine int with typedef, Can enhance the portability of your code.
This article is from the "13088918" blog, please be sure to keep this source http://13098918.blog.51cto.com/13088918/1945211
A typedef of the trap of C language