How can I have a pointer to the next struct in the definition of this struct:
如何在这个结构的定义中有指向下一个结构的指针:
typedef struct A {
int a;
int b;
A* next;
} A;
this is how I first wrote it but it does not work.
这是我第一次写它,但它不起作用。
6 个解决方案
#1
59
You can define the typedef and forward declare the struct first in one statment, and then define the struct in a subsequent definition.
您可以定义typedef,并在一个状态中首先声明struct,然后在随后的定义中定义struct。
typedef struct A A;
struct A
{
int a;
int b;
A* next;
};
Edit: As others have mentioned, without the forward declaratation the struct name is still valid inside the struct definition (i.e. you can used struct A
), but the typedef is not available until after the typedef definition is complete (so using just A
wouldn't be valid). This may not matter too much with just one pointer member, but if you have a complex data structure with lots of self-type pointers, may be less wieldy.
编辑:正如其他人所提到的,在没有前向声明的情况下,struct名称在struct定义中仍然有效(例如,您可以使用struct A),但是类型定义在typedef定义完成之后才可用(因此只使用A是无效的)。对于一个指针成员来说,这可能不太重要,但是如果您有一个复杂的数据结构,其中有许多自类型指针,那么它可能就不那么笨重了。
#2
53
In addition to the first answer, without a typedef and forward declaration, this should be fine too.
除了第一个答案之外,没有类型定义和转发声明,这也应该没问题。
struct A
{
int a;
int b;
struct A *next;
};
#3
17
You are missing the struct
before the A*
你错过了A*之前的结构
typedef struct A {
int a;
int b;
struct A* next;
} A;
#4
12
You can go without forward declaration:
你可以不提前声明:
struct A {
int a;
int b;
struct A *next;
};
#5
7
Please, you're in C, not C++.
拜托,你用的是C语言,不是c++。
If you really must typedef a struct (and most programmers that I work with would not¹), do this:
如果你真的必须类型定义一个结构体(和大多数程序员,我的工作不会¹),这样做:
typedef struct _A {
int a;
int b;
struct _A *next;
} A;
to clearly differentiate between _A
(in the struct
namespace) and A
(in the type namespace).
要明确区分_A(在struct名称空间中)和A(在类型名称空间中)。
¹typedef
hides the size and storage of the type it points to ― the argument (and I agree) is that in a low-level language like C, trying to hide anything is harmful and counterproductive. Get used to typing struct A
whenever you mean struct A
.
¹typedef隐藏的大小和存储类型它指向——(我认为)的观点认为,在一个低级语言(如C),试图隐瞒什么是有害的,会适得其反。习惯输入struct A,只要你指的是struct A。
#6
0
typedef struct {
values
} NAME;
This is shorter way to typedef a struct i think its the easiest notation, just don't put the name infront but behind.
这是定义结构的一种更短的方式我认为这是最简单的符号,只是不要把名字放在前面而放在后面。
you can then call it like
你可以这样称呼它
NAME n;
NAME *n; // if you'd like a ptr to it.
Anything wrong with this approach?
这种方法有什么问题吗?
#1
59
You can define the typedef and forward declare the struct first in one statment, and then define the struct in a subsequent definition.
您可以定义typedef,并在一个状态中首先声明struct,然后在随后的定义中定义struct。
typedef struct A A;
struct A
{
int a;
int b;
A* next;
};
Edit: As others have mentioned, without the forward declaratation the struct name is still valid inside the struct definition (i.e. you can used struct A
), but the typedef is not available until after the typedef definition is complete (so using just A
wouldn't be valid). This may not matter too much with just one pointer member, but if you have a complex data structure with lots of self-type pointers, may be less wieldy.
编辑:正如其他人所提到的,在没有前向声明的情况下,struct名称在struct定义中仍然有效(例如,您可以使用struct A),但是类型定义在typedef定义完成之后才可用(因此只使用A是无效的)。对于一个指针成员来说,这可能不太重要,但是如果您有一个复杂的数据结构,其中有许多自类型指针,那么它可能就不那么笨重了。
#2
53
In addition to the first answer, without a typedef and forward declaration, this should be fine too.
除了第一个答案之外,没有类型定义和转发声明,这也应该没问题。
struct A
{
int a;
int b;
struct A *next;
};
#3
17
You are missing the struct
before the A*
你错过了A*之前的结构
typedef struct A {
int a;
int b;
struct A* next;
} A;
#4
12
You can go without forward declaration:
你可以不提前声明:
struct A {
int a;
int b;
struct A *next;
};
#5
7
Please, you're in C, not C++.
拜托,你用的是C语言,不是c++。
If you really must typedef a struct (and most programmers that I work with would not¹), do this:
如果你真的必须类型定义一个结构体(和大多数程序员,我的工作不会¹),这样做:
typedef struct _A {
int a;
int b;
struct _A *next;
} A;
to clearly differentiate between _A
(in the struct
namespace) and A
(in the type namespace).
要明确区分_A(在struct名称空间中)和A(在类型名称空间中)。
¹typedef
hides the size and storage of the type it points to ― the argument (and I agree) is that in a low-level language like C, trying to hide anything is harmful and counterproductive. Get used to typing struct A
whenever you mean struct A
.
¹typedef隐藏的大小和存储类型它指向——(我认为)的观点认为,在一个低级语言(如C),试图隐瞒什么是有害的,会适得其反。习惯输入struct A,只要你指的是struct A。
#6
0
typedef struct {
values
} NAME;
This is shorter way to typedef a struct i think its the easiest notation, just don't put the name infront but behind.
这是定义结构的一种更短的方式我认为这是最简单的符号,只是不要把名字放在前面而放在后面。
you can then call it like
你可以这样称呼它
NAME n;
NAME *n; // if you'd like a ptr to it.
Anything wrong with this approach?
这种方法有什么问题吗?