Such as:
typedef int INTEGER;
typedef float REAL;
Specifies that integer represents the int type, and real represents the float type, so that the following two lines are equivalent:
1 int i,j; float a,b;
2) INTEGER i,j; Real A,b;
You can declare a struct type:
Copy Code code as follows:
typedef struct
{
int month;
int day;
int year;
}date;
When you declare a new type date, you can use the date definition variable: Date birthday (do not write struct date birthday;); date* p;//pointer to struct type.
can also be further:
1) typedef int NUM[10];//Declare integer array type
NUM n;//defines n as an integer array variable, where n[0]--n[9] is available
2) typedef char* STRING;//Declare STRING as character pointer type
STRING p,s[10];//p is a character pointer variable and s is an array of pointers
3) typedef int (*pointer) ();//Declaration pointer is a pointer type that points to a function that returns an integer value with no parameters
Pointer p1,p2;//p1,p2 as a pointer variable of type pointer
Description
1 use typedef can declare various types of names, but can not be used to define variables, with typedef can declare array type, string type, easy to use.
For example: Define an array, which used to be: int a[10],b[10],c[10],d[10]; because they are all one-dimensional arrays and the same size, you can declare this array type as a first name:
typedef int ARR[10];
Then use ARR to define the array variables:
ARR A,b,c,d;//arr is an array type that contains 10 elements. So a,b,c,d is defined as a one-dimensional array with 10 elements. As you can see, you can isolate array types and arrays by using a TypeDef, and you can define multiple array variables by using an array type. You can also define string types, pointer types, and so on.
2 use typedef to simply add a type name to an existing type without creating a new type.
3 the typedef and #define have similarities, but in fact the two are different, #define是在 precompiled processing, it can only do a simple string substitution, and the typedef is at compile-time processing. Rather than doing a simple string substitution, it declares a type as a method of defining a variable.
For example: typedef int COUNT, and #define COUNT int are all used to represent int, but in fact they are different.
4 when the same type of data is used in different source files, a common typedef declares some data types, puts them in a single file, and then includes them in a file that needs to be used with the #include command.
5) typedef is beneficial to the general and transplant of programs.