14. typedef command


Fourteen, typedef command

1. Introduction to typedefs

The typedef command is used to alias a type.

typedef type name;

In the above code, type represents the type name, and name represents the alias.

typedef unsigned char BYTE;
BYTE c = 'z';

In the above example, the typedef command aliases the type unsign char to BYTE, and then you can use BYTE to declare variables.
A typedef can specify multiple aliases at once.

typedef int antelope, bagel, mushroom;

In the above example, three aliases are given to the int type at once.
typedefs can alias pointers.

typedef int* intptr;
int a = 10;
intptr x = &a;

In the example above, intptr is an alias for int*. However, be careful when using it, it is not easy to see that the variable x is a pointer type.
typedefs can also be used to alias array types.

typedef int five_ints[5];
five_ints x = {
    
    11, 22, 33, 44, 55};

In the above example, five_ints is an array type, and the typedef containing 5 integers is written as an alias for the function as follows.

typedef signed char (*fp)(void);

In the above example, the type alias fp is a pointer representing the function signed char (*)(void).

2. The main benefits of typedef

The benefits of typedef aliasing types mainly include the following points.

(1) Better code readability.

typedef char* STRING;
STRING name;

The above example aliases the character pointer as STRING. When STRING is used to declare a variable later, it can be easily identified that the variable is a string.

(2) Create aliases for complex data structures defined by commands such as struct, union, enum, etc., so as to facilitate reference.

struct treenode {
    
    
  // ...
};
typedef struct treenode* Tree;

In the above example, Tree is an alias of struct treenode*.
typedef can also be written together with struct commands to define data types.

typedef struct animal {
    
    
  char* name;
  int leg_count, speed;
} animal;

In the above example, when customizing the data type, the typedef command is used at the same time to create an alias animal for struct animal.
In this case, the C language allows omission of the type name following the struct command.

typedef struct {
    
    
  char *name;
  int leg_count, speed;
} animal;

The above example is equivalent to aliasing an anonymous data type animal.

(3) typedef is convenient for changing the type of the variable in the future.

typedef float app_float;
app_float f1, f2, f3;

In the above example, the types of variables f1, f2, and f3 are all float. If you need to change the type for them in the future, you only need to modify the typedef statement.

typedef long double app_float;

The above command changes the types of the variables f1, f2, and f3 to long double.

(4) Portability

The type of a value on different computers may be different.

int i = 100000;

The above code works fine on computers with 32-bit integers, but it will fail on computers with 16-bit integers.
The solution of C language is to provide type aliases, which will be interpreted as different types on different computers, such as int32_t.

int32_t i = 100000;

The above example declares the variable i as int32_t type to ensure that it has a 32-bit width on different computers, and there will be no error when porting the code.
Type aliases in this category are defined with typedefs. Below is a similar example.

typedef long int ptrdiff_t;
typedef unsigned long int size_t;
typedef int wchar_t;

These integer type aliases are all placed in the header file stdint.h. Computers with different architectures only need to modify this header file without modifying the code.
Therefore, typedef helps to improve the portability of code so that it can be adapted to computers of different architectures.

(5) Simplified type declaration

Some type declarations in C language are quite complicated, such as the following one.

char (*(*x(void))[5])(void);

typedefs can simplify complex type declarations, making them easier to understand. First, the outermost layer starts with a type alias.

typedef char (*Func)(void);
Func (*x(void))[5];

This still looks a bit complicated, just define an alias for the inner layer.

typedef char (*Func)(void);
typedef Func Arr[5];
Arr* x(void);

The code above is easier to read.

  • x is a function that returns a pointer to type Arr.
  • Arr is an array with 5 members, each member is of type Func.
  • Func is a function pointer to a function that takes no arguments and returns a character value.

Guess you like

Origin blog.csdn.net/qq_40670171/article/details/130072182