Introduction to C Language Learning (9) typedef keywords
Keyword: typedef
Usage: define a new name (alias) for various data types)
Typedef and basic data types
Typedef int Integer; Integer a = 8;
You can also create another alias Based on the alias.
Typedef Integer MyInteger; MyInteger a = 8;
The original data type can also be used normally.
Typedef and pointer
Typedef char * String; String str = "jackie ";
Typedef andStruct
TypedefstructPerson Per; // you do not need to include the struct keyword when defining the struct variable.
Per p; p. name = "xyz ";
Define and take the alias:
Typedefstruct Student // The struct name Student can be omitted.
{
Int age;
} Stu;
Void processStudent ()
{
Stu student = {18 };
Student. age = 19;
}
Typedef and pointStructPointer
Typedef struct
{
Int age;
} Stu;
Stu stu = {20 };
Typedef Stu * S; // the alias S for the pointer to the struct.
S s = & stu;
Typedef struct LNode
{
Int data;
Struct LNode * next;
} LinkList, * SList;
Int main (int argc, const char * argv [])
{
LinkList l = {1, NULL };
LinkList ll = {2, NULL };
L. next = & ll;
Printf ("% d,", l. next-> data );
SList sl = & ll;
If (sl-> next! = NULL)
Printf ("% d,", sl-> data );
Return 0;
}
Typedef andEnumeration type
Typedef enum
{
...
} Season;
// Similar to struct
Typedef andPointer to function
Int sum (int a, int B)
{
Return a + B;
}
Void main ()
{
Typedef int (* P) (int a, int B );
P p = sum;
Int result = (* p) (3, 5 );
Return 0;
}
Typedef and # define
Typedef char * String;
String s = "abc"
# Define String char *;
String s = "abc"; // The effect is the same
To use this method:
String s1, s2; // Replace the first one with char * s1, char * s2;
String s3, s4; // replace char * s3, s4; <=> char * s3, char s4;