1. Basic use
The use of 1> typedef in basic data types
typedef int MYINT; equals an alias to int.
typedef MYINT MYINT2; It's the equivalent of an alias for MyInt.
MyInt a = 10; Myint equivalent to int
MyInt2 B = 12; MyInt2 equivalent to int
2> typedef and pointers when used together
Char *name = "Jack";
typedef char * string;//equivalent to (char *) a name string
String name1 = "Jame";
3> the use of typedef and structs together
①struct Date {int year; int month; int day};
typedef struct DATE mydate;//is equivalent to an alias for struct date
②typedef truct Date {int year; int month, int day} mydate;//another way to use
③typedef truct {int year; int month, int day} mydate;//another way to use
4> typedef and enumeration when used together
Enum Sex {Sexman,sexwoman,unknow};
typedef enum SEX {Sexman,sexwoman,unknow} mysex;//is equivalent to an alias for an enumeration type
Mysex s = sexman;//defining variables
5> typedef and function pointers when used together
int sum (int a, int b)
{
return a + B;
}//first defines a function
Int (*p) (int, int);//defines a pointer type that points to a function
typedef int (*mypoint) (int, int);//Pointer to function the type of a name MyPoint
MyPoint p = sum;
6> typedef and struct pointers when used together
struct Date {int year; int month; int day};//defines a struct type
struct Date d = {1990, 10, 10};//defines struct-body variables
struct Date *p = &d;//defines a pointer to a struct variable
typedef struct DATE *datepoint;
Datepoint P2 = &d;
typedef struct DATE {int year; int month; int day} * dstepoint;//another way of defining
7> Summary
① function: A new name for an already existing type
② Use occasions:
1) Basic Data type
2) Structural body
3) pointer
4) enumeration
5) Pointer to function
2. Use note
1> typedef and macro definitions are used at the same time, note that the macro definition is text substitution
#denfine string2 char *
Type char * string;
String s1,s2; S1, S2 are all char * types
String2 S3,s4; S3 is a char * type, S4 is a char type, because the macro is a text substitution
C Language-keyword typedef