[英]Wrong syntax for typedef with struct?
I have the following code below. 我下面有以下代码。
typedef struct person Person;
Person {
char* name;
int age;
};
From what I understand, typedef will substitute "struct person" with Person. 据我了解,typedef将用Person代替“ struct person”。 So when making the struct, it is equal to writing : 因此,在构造该结构时,它等于编写:
struct person {
char* name;
int age;
};
Is this thinking correct? 这种想法正确吗? Because I am getting an error an error of the first line of the struct. 因为出现错误,所以结构的第一行出现了错误。
error: expected identifier or '(' before '{' token This error is referring to the line : Person { 错误:预期标识符或'{'标记前的'('。此错误是指以下行: Person {
Any help is appreciated. 任何帮助表示赞赏。 Thanks 谢谢
Do it like this 像这样做
typedef struct person Person;
struct person {
char* name;
int age;
};
Then you can use Person
for all usages of the struct
. 然后,可以将Person
用于struct
所有用法 。
also there is no need in different capitalization 也不需要大写
typedef struct person person;
would do equally well. 也会做得很好。
One way to do what you want is: 一种执行所需操作的方法是:
struct person {
char* name;
int age;
};
typedef struct person Person;
Or, if you want to accomplish this in one instruction, you could do: 或者,如果您想在一条指令中完成此操作,则可以执行以下操作:
typedef struct person {
char* name;
int age;
} Person;
A possible way is as follows: 可能的方法如下:
typedef struct {
char* name;
int age;
} Person;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.