- C language
- Basic grammar
- here
typedef statement-create a type alias
You can use a typedef statement to create a type alias.
typedef typename type alias
Create another name for the structure
Let's create an alias for the structure. Name the structure "struct spvm_field" "SPVM_FIELD".
#include <stdint.h>
// Structure definition
struct spvm_field {
int32_t id;
const char * name;
};;
// Create another name for the structure
typedef struct spvm_field SPVM_FIELD;
A structure is a type. It can be specified as the type name of a typedef statement.
Create alias for shared type
Let's create an alias for the union type. Name the shared type "union spvm_value" "SPVM_VALUE".
#include <stdint.h>
// Union definition
union spvm_value {
int8_t bval;
int16_t sval;
int32_t ival;
int64_t lval;
float fval;
double dval;
void * oval;
};;
// Create a union alias
typedef union spvm_value SPVM_VALUE;
Unions are a type. It can be specified as the type name of a typedef statement.
How to give a type alias before defining a structure or union
The above method used a typedef statement to create a type alias after defining a structure or union.
Is there a way to change this order?
Let's add a type alias before defining a structure or union.
It uses the C language specification that only the names of structures and unions can be declared first.
#include <stdint.h>
// Create another name for the structure
struct spvm_field;
typedef struct spvm_field SPVM_FIELD;
// Structure definition
struct spvm_field {
int32_t id;
const char * name;
};;
// Create a union alias
union spvm_value;
typedef union spvm_value SPVM_VALUE;
// Union definition
union spvm_value {
int8_t bval;
int16_t sval;
int32_t ival;
int64_t lval;
float fval;
double dval;
void * oval;
};;
So what does this mean?
This is useful if you want to create a type declaration header to reduce inter-header loading.
When is the typedef statement used?
When is the typedef statement used?
To abstract the type
Typedef statements are sometimes used to abstract types.
For example, the size_t type is used in C to express length, but the definition may differ between the 32-bit environment and the 64-bit environment.
This is because size_t is defined in the C language specification as "at least 16-bit unsigned integer type".
If you follow the C language specifications, you can change the definition depending on the processing system.
typedef unsigned int size_t; typedef unsigned long size_t; typedef unsigned long long size_t;
The actual definition differs depending on the processing system, but it can be abstracted as size_t in the source code.
Because it is troublesome to write structs and unions
If you want to treat it as a type name in a unified way, writing "struct honya" and "union honya" feels a little troublesome.
If you use a typedef, you can treat it like a single type name.
To eliminate cross-references and circular references in headers
You can avoid cross-references and circular references in headers by creating a type declaration header.
I think this is very important for the practical use of the typdef statement.
Where do you write the typedef statement?
Write the typedef statement in the header file.
C Language Zemi