gpt4 book ai didi

c - 无法解析代码中的结构内容

转载 作者:行者123 更新时间:2023-11-30 16:48:54 25 4
gpt4 key购买 nike

我试图在结构中累积所有类型的列表,并从该结构中引用每个列表,而不是使用结构的不同名称

    #include <stdio.h>
#define TRUE 1
#define FALSE 0

typedef struct test_s {
int val;
int p;
}test_t;

test_t list_1[] = {
{ TRUE, 1},
{ TRUE, 2},
{ TRUE, 3}
};

test_t list_2[] = {
{FALSE, 1},
{TRUE , 2},
{TRUE , 3}
};

test_t list_types[][] = { list_1, list_2 };
test_t *list = NULL;

void
main() {

list = list_types[0];

int iter = 0;
for ( iter = 0; iter < sizeof(list)/sizeof(list[0]); iter++){
printf("Bool: %d Val: %d\n",list[0].val, list[0].p);
}
}

我是当我尝试编译时,出现以下错误。请突出显示我在这段代码中缺少的内容

test.c:22:8: error: array type has incomplete element type

最佳答案

事实上,您想要创建一个结构指针数组。

第 1 步 - 将数组声明并初始化为 using。

test_t *list_types[] = { list_1, list_2 };

而不是:

test_t list_types[][] = { list_1, list_2 };

第 2 步 - 使用该数组而不是指向该数组的指针来确定当前大小。

And use the index iter to select between list_1 or list_2, otherwise you will access to the list_1.

To access to all items of one list_<x>, it is necessary to store the size.

for ( iter = 0; iter < sizeof(list_types)/sizeof(list_types[0]); iter++){
list = list_types[iter];
printf("Bool: %d Val: %d\n",list[0].val, list[0].p);
}

而不是:

for ( iter = 0; iter < sizeof(list)/sizeof(list[0]); iter++){
printf("Bool: %d Val: %d\n",list[0].val, list[0].p);
}

关于c - 无法解析代码中的结构内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42819696/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com