C 结构中的不完整类型
C incomplete Type in structs
伙计们,
我有一个问题和一个问题。
希望你能帮我解释一下。
首先我有两个结构:
typedef struct {
double x;
double y;
} A;
typedef struct {
unsigned int count;
A(*stack)[];
}B;
我在 main() 中声明了这个结构 B,并将 B 的指针传递给将初始化的函数
main(){
B x;
function rr(&x);
}
void rr(B* test) {
test->stack= malloc((4) * sizeof(A)); //4Elements
for (unsigned int i = 0; i < 4; i++) {
(test->stack+ i)->x= 89;
}
}
在这一行
(test->stack+ i)->x= 89;
编译器说类型不完整
我知道为什么它在结构 B 中是不完整的,因为它们没有维度。
但是数组应该在函数 rr
中初始化
也许你明白我的意思以及如何解决我的问题。
功能rr我不允许更改。
问候
编辑 1
谢谢大家的回答
也许我应该解决我的问题
typedef struct {
unsigned int count;
A(*stack)[]; // here i want a pointer to an array of A's
}B;
//over main it is declared
void rr(B*);
main(){
B x;
function rr(&x);
}
// this func is not allowed to change
void rr(B* test) {
test->stack= malloc((4) * sizeof(A)); //4Elements
for (unsigned int i = 0; i < 4; i++) {
(test->stack+ i)->x= 89; // error cause incomplete type but i
//have to use this line
}
}
希望现在我更容易理解我想要什么
此声明:
A(*stack)[];
表示 stack
是指向未知大小的 A
数组的指针。这是一个不完整的类型,这意味着它不能直接使用。
看来你真正想要的不是指向数组的指针,而是指向A
动态数组第一个成员的指针。所以将成员声明为指针:
A *stack;
表达式中:
(test->stack+ i)->x= 89;
在通过指向数组的指针访问数组之前,您必须取消引用它。
尝试:
(*test->stack)[i].x= 89;
你不知道如何使用灵活的数组成员。
简单地说:
typedef struct {
double x;
double y;
} A;
typedef struct {
size_t count;
A stack[];
}B;
B *createStack(size_t size)
{
B *s = malloc(sizeof(*s) + size * sizeof( s -> stack[0]));
return s;
}
void rr(B* test) {
for (unsigned int i = 0; i < 4; i++) {
(test->stack+ i)->x= 89;
}
}
int main(void)
{
B *stack = createStack(4);
rr(stack);
free(stack);
}
您只需要对 mallloc/realloc 进行一次分配或释放结构。该数组将衰减为您在 rr
函数中分配的指针。
伙计们, 我有一个问题和一个问题。 希望你能帮我解释一下。
首先我有两个结构:
typedef struct {
double x;
double y;
} A;
typedef struct {
unsigned int count;
A(*stack)[];
}B;
我在 main() 中声明了这个结构 B,并将 B 的指针传递给将初始化的函数
main(){
B x;
function rr(&x);
}
void rr(B* test) {
test->stack= malloc((4) * sizeof(A)); //4Elements
for (unsigned int i = 0; i < 4; i++) {
(test->stack+ i)->x= 89;
}
}
在这一行 (test->stack+ i)->x= 89; 编译器说类型不完整 我知道为什么它在结构 B 中是不完整的,因为它们没有维度。 但是数组应该在函数 rr
中初始化也许你明白我的意思以及如何解决我的问题。 功能rr我不允许更改。
问候
编辑 1 谢谢大家的回答 也许我应该解决我的问题
typedef struct {
unsigned int count;
A(*stack)[]; // here i want a pointer to an array of A's
}B;
//over main it is declared
void rr(B*);
main(){
B x;
function rr(&x);
}
// this func is not allowed to change
void rr(B* test) {
test->stack= malloc((4) * sizeof(A)); //4Elements
for (unsigned int i = 0; i < 4; i++) {
(test->stack+ i)->x= 89; // error cause incomplete type but i
//have to use this line
}
}
希望现在我更容易理解我想要什么
此声明:
A(*stack)[];
表示 stack
是指向未知大小的 A
数组的指针。这是一个不完整的类型,这意味着它不能直接使用。
看来你真正想要的不是指向数组的指针,而是指向A
动态数组第一个成员的指针。所以将成员声明为指针:
A *stack;
表达式中:
(test->stack+ i)->x= 89;
在通过指向数组的指针访问数组之前,您必须取消引用它。 尝试:
(*test->stack)[i].x= 89;
你不知道如何使用灵活的数组成员。
简单地说:
typedef struct {
double x;
double y;
} A;
typedef struct {
size_t count;
A stack[];
}B;
B *createStack(size_t size)
{
B *s = malloc(sizeof(*s) + size * sizeof( s -> stack[0]));
return s;
}
void rr(B* test) {
for (unsigned int i = 0; i < 4; i++) {
(test->stack+ i)->x= 89;
}
}
int main(void)
{
B *stack = createStack(4);
rr(stack);
free(stack);
}
您只需要对 mallloc/realloc 进行一次分配或释放结构。该数组将衰减为您在 rr
函数中分配的指针。