Let's talk about those things of typedef and those things of typedef.
Recently, when reviewing data structures, I often see typedef, but I still don't know how to use typedef. For details, refer to C Primer Plus 5th. and write the content for your communication and learning.
The typedef tool is an advanced data feature that enables you to create your own name for a type. In this respect, it is similar to # define, but they have three differences:
- Different from # define, the symbol name given by typedef is limited to the pair type rather than the pair value.
- Typedef is interpreted by the compiler rather than the interpreter.
- Although its range is limited, typedef is more flexible than # define.
Let's see how typedef works. If you want to use the term BYTE for the 1-BYTE value, you only need to define the BYTE as if you were defining a char variable, and then add the canonicalized word typedef before the definition, for example:
typedef unsigned char BYTE;
Then you can use BYTE to define variables:
BYTE x, y[10], *z;
The scope of the definition depends on the position of the typedef statement. If it is defined within a function, its scope is local in the county and limited to that function. If the definition is external to the function, it will have a global scope.
Generally, these definitions use uppercase letters to remind users that this type of name is actually abbreviated. However, you can also use lowercase letters:
typedef unsigned char byte;
The same rule for managing variable names is also used to manage the names used by typedef.
Creating a name for an existing type may seem unnecessary, but this may be useful. In the previous example, replacing unsigned char with BYTE helps indicate that you intend to use the BYTE variable to represent the value rather than the character encoding. Using typedef also helps increase portability. For example, we have mentioned the size_t type that represents the sizeof operator return type and the time_t type that represents the return value type of the function time. Standard C specifies that sizeof and time () should return the integer type, but it is left to the specific implementation to determine the exact Integer type. The reason for not specifying it is that the ansi c committee does not think any of them is the best choice for all computer platforms. Therefore, they propose a new type name, such as time_t, so that C can use typedef to set this name to a specific data type. In this way, they can provide the following general prototypes:
time_t time(time_t *);
In one system, time_t may be of the unsigned int type; in another system, it may be of the unsigned long type. As long as the time. h header file is included, the program can access the appropriate definition. You can also declare the time_t variable in the code.
Use # define to implement some functions of typedef. For example:
#define BYTE unsigned char
This enables the Preprocessor to replace BYTE with unsigned char. But there are also some functions that cannot be implemented by # define, as shown in the following example:
typedef char* STRING;
If there is no keyword typedef, this example recognizes STRING as a char pointer. With this keyword, the STRING is the identifier of the char pointer. Therefore:
STRING name, sign;
Meaning:
char * name, * sign;
However, let's assume that:
#define STRING char *;
So:
STRING name, sign;
It will be translated into the following form:
char *name,sign;
In this case, only name is a pointer.
You can also use typedef for the structure:
typedef struct complex{ float real; float imag;}COMPLEX;
In this way, you can replace struct COMPLEX with complex to represent the plural. One of the reasons for using typedef is to create a convenient and identifiable name for frequently occurring types. For example, in the previous example, many people prefer to use STRING or equivalent tags.
When you use typedef to name a structure type, you can save the structure Tag:
typedef struct{ double x; double y;}rect;
Assume that the type name defined by typedef is used as follows:
rect r1 = {3.0, 6.0};rect r2;r2 = r1;
This is translated:
1 struct{ 2 double x; 3 double y; 4 }r1 = {3.0, 6.0}; 5 6 struct{ 7 double x; 8 double y; 9 }r2;10 r2 = r1;
If the declaration of both structures does not use tags, but uses the same members (the member name and type are matched), C considers the two structures of the same type, therefore, assigning r1 to r2 is a correct operation.
Another reason for using typedef is that the name of typedef is often used for complex types. For example:
Typedef char (* FRPTC () [5];
In this case, FRPTC is declared as a function type. A function of this type returns a pointer to a char array containing five elements (see some of the odd declarations to be discussed below ).
When typedef is used, remember that it does not create a new type. It only creates easy-to-use labels. This means that, for example, the security of the STRING parameter we created can be passed as a parameter to a function that requires a char pointer type parameter.
Through structure, union, and typedef, C provides an effective and convenient tool for data processing.
Author: xin, published in blog
Reprinted please indicate the source, welcome to mail exchange: zhuanxinxin@foxmail.com