Key Words: 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 alias an alias based on the
typedef Integer Myinteger; Myinteger AA = 8;
The original data type can also be used normally
typedef with the pointer
typedef Char *String; String str = "stone";
typedef with the structural body
typedefstruct person Per; // This will not bring up the struct keyword when defining struct variables.
Per p; P.name = "xyz";
Define and take aliases:
typedef struct Student//struct name Student can be omitted
{
int age;
} Stu;
void processstudent ()
{
Stu student = {+};
Student. age =+;
}
typedef and Point Structural Body the pointer
typedef struct
{
int age;
} Stu;
Stu Stu = {20};
typedef STU *S; //Pointer to struct to take alias S
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 = ≪
printf ("%d,", l.next->data);
SList SL = ≪
if (sl->next! = NULL)
printf ("%d,", sl->data);
return 0;
}
typedef with enum types
typedef enum
{
...
} Season;
Usage is similar to structural body
typedef with the pointers to functions
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 with the #define
typedef char *string;
String s = "abc"
#define String char *;
String s = "abc"; This uses the same effect
When this is used:
String s1,s2; //With the first substitution: Char *s1, char *s2;
String S3,s4; //With second substitution: char * S3, S4; <==> Char *s3, Char S4;
Introduction to C language Learning (eight) typedef keyword