The C language allows for a new alias for a data type, just like a nickname for a person.
The purpose of alias is not to improve the efficiency of the program, but to encode conveniently. For example, if you have a struct whose name is Stu, you want to define a struct variable by writing:
struct Stu stu1;
struct seems superfluous, but does not write and will report an error. If you have an alias for struct Stu Stu, it's easy to write:
STU stu1;
This kind of writing is more concise, the meaning is also very clear, whether in the standard header file or later programming practice, will use this alias.
Use the keyword typedef to make a new alias for a type, with the syntax format:
typedef oldname NEWNAME;
Oldname is the original name of the type, NewName is the type of the new name. For example:
typedef int INTEGER;
INTEGER A, B;
A = 1;
b = 2;
INTEGER A, B; is equivalent to int a, b;.
A typedef can also define aliases for types such as arrays, pointers, and struct bodies. Let's take a look at an example that defines an alias for an array type:
typedef char ARRAY20[20];
Indicates that ARRAY20 is an alias for type char [20]. It is an array type of length 20. You can then define the array with ARRAY20:
ARRAY20 A1, a2, S1, S2;
It is equivalent to:
Char a1[20], a2[20], s1[20], s2[20];
Note that arrays are also of type. For example, Char a1[20]; defines an array A1, its type is char [20], which has been explained in the VIP tutorial "arrays and pointers are by no means equivalent, arrays are another type".
Also, for example, define an alias for a struct type:
typedef struct stu{
char name[20];
int age;
char sex;
} STU;
STU is an alias for struct STU, you can define a struct variable with STU:
STU Body1,body2;
It is equivalent to:
struct Stu body1, body2;
Again, define an alias for the pointer type:
typedef int (*ptr_to_arr) [4];
Indicates that Ptr_to_arr is an alias of type int * [4], which is a two-dimensional array pointer type. You can then use Ptr_to_arr to define a two-dimensional array pointer:
Ptr_to_arr P1, p2;
In a similar way, you can also define aliases for function pointer types:
typedef int (*PTR_TO_FUNC) (int, int);
Ptr_to_func Pfunc;
The example defines the alias for the pointer.
#include <stdio.h>
typedef char (*ptr_to_arr) [m];
typedef int (*PTR_TO_FUNC) (int, int);
int max (int a, int b) {return
a>b a:b;
}
Char str[3][30] = {"
http://c.biancheng.net",
"C language Chinese net",
"C-language"
};
int main () {
Ptr_to_arr parr = str;
Ptr_to_func pfunc = max;
int i;
printf ("Max:%d\n", (*pfunc));
For (i=0 i<3; i++) {
printf ("str[%d]:%s\n", I, * (Parr+i));
}
return 0;
}
Run Result:
Max:20
Str[0]: http://c.biancheng.net
STR[1]: C language Chinese web
STR[2]: C-language
It should be emphasized that the TypeDef is giving the existing type a new name, rather than creating a new type. To "See the name", try to use an unambiguous identifier and capitalize as much as you could.
The difference between a typedef and a #define
A typedef sometimes behaves like a #define, but there is a key difference between it and a macro substitution. The way to think about this problem correctly is to look at the typedef as a complete "encapsulation" type, and you can't add anything else to it after the declaration.
1 The macro type name can be extended using a different type descriptor, but not the type name defined by the typedef. As shown below:
#define Interge INT
unsigned interge n; No problem
typedef int Interge;
unsigned interge n; Error, cannot add unsigned in front of Interge
2 When several variables are defined continuously, the TypeDef can guarantee that all the variables defined are of the same type, while the #define is not guaranteed. For example:
#define PTR_INT INT *
Ptr_int P1, p2;
After the macro is replaced, the second line becomes:
int *P1, p2;
This makes P1, p2 a different type: P1 is a pointer to an int type, and p2 is an int type.
Instead, in the following code:
typedef int * Ptr_int
Ptr_int P1, p2;
P1, p2 types are the same, they are pointers to type int.
The above is on the C language TypeDef data collation, thank you all for the support of this site!