Typedef in c language can define new types. Very convenient

Well, this knowledge was told to me by chatgpt. There is no relevant explanation in the c language tutorial. Let’s
first look at the structure definition

struct Point {
    
    
	int x;
	int y;
	int nfcCardCode;
	bool isObstacle;
} pointA;

In this C language code, a Point type structure is defined, and a Point type variable is initialized at the same time. It is called pointA

At the same time, we can continue to define other variables

struct Point  pointB;
struct Point  pointC;
struct Point  pointD;
struct Point  pointE;

Note that there must be a struct keyword in front of it, otherwise the compiler will prompt you that you don’t know what Point is. It is treated as a macro by default.
Such a definition will inevitably lead to the need to add a struct in front of any place where the structure is used
. Is there any syntax to fix this nasty problem?

There is a method, which is the protagonist of today. typedef

typedef struct {
    
    
	int x;
	int y;
	int nfcCardCode;
	bool isObstacle;
} Point;struct Point {
    
    
	int x;
	int y;
	int nfcCardCode;
	bool isObstacle;
} Point;

的区别是什么?

The difference between these two definitions is that the first one uses the typedef keyword to define a new type name Point, while the second one directly defines a structure type Point.

The typedef keyword can be used to define a new name for an existing type, so that the type can be used conveniently without having to write out the complete type name every time. In this example, the typedef keyword defines struct Point as a new type Point, so that Point can be directly used instead of struct Point in the code.

The second definition directly defines a structure type Point, which needs to be represented by struct Point when using this type.

Typedef is the C language syntax introduced by which version?

typedef is an early syntax of the C language, and its function is to define a new name for an existing type. In the C language, the typedef keyword can be used to define a new type name, so that the type can be used conveniently without having to write out the complete type name every time.

The typedef keyword is used as follows:

typedef <已有类型> <新类型名>;

Among them, the existing type can be any existing type, including basic type, structure, union, enumeration, etc., and the new type name is a new name defined for the type.

The use of the typedef keyword is very flexible and can be used to define various type aliases, for example:

typedef int Integer;
typedef float Real;
typedef struct Point Point;

These statements respectively define three new type names Integer, Real and Point, which respectively represent the three existing types of int, float and struct Point.

grammar

typedef struct {
    
    
	int x;
	int y;
	int nfcCardCode;
	bool isObstacle;
} Point;

可以写成

typedef struct Point{
    
    
	int x;
	int y;
	int nfcCardCode;
	bool isObstacle;
} ;?

Can't. When using typedef to define a new type name, both the existing type and the new type name must be specified, and neither of them can be omitted. Therefore, the correct way of writing should be:

typedef struct Point {
    
    
    int x;
    int y;
    int nfcCardCode;
    bool isObstacle;
} Point;
或者
// 这里名称不写就是匿名类型名
typedef struct {
    
    
    int x;
    int y;
    int nfcCardCode;
    bool isObstacle;
} Point;

Among them, struct Point is an existing type, and Point is a new type name defined for this type. In this example, we use the typedef keyword to define struct Point as a new type Point, so that we can directly use Point instead of struct Point in the code.
You can directly write like this when you define structure variables in the future.

 Point  pointB;
 Point  pointC;
 Point  pointD;
 Point  pointE;

Damn. After writing code for more than 10 years, I don’t even know that C language has such a grammar. It seems that this grammar is not written in the tutorial. It’s not as good as the knowledge of artificial intelligence for a few days.

to be eliminated.

Guess you like

Origin blog.csdn.net/phker/article/details/131188429