代码:
struct bunchofdata
{
int i;
void *dllist[i];
int spltq[i];
pthread_t tlist[i];
};
错误消息:
error: ‘i’ undeclared here (not in a function)
void *dllist[i];
^
我不明白为什么这不起作用。
最佳答案
除了 pikkewyn's answer ,实现此目的的另一种方法是使用 Flexible Array Member
这比单独分配成员更容易管理。
#include <stdio.h>
#include <stdlib.h>
typedef int pthread_t;
struct data
{
void *dl;
int spltq;
pthread_t thread;
};
struct bunchofdata
{
int i;
struct data data_list[];
};
struct bunchofdata * data_factory(int size)
{
struct bunchofdata * ret = malloc(sizeof(struct bunchofdata)
+size*sizeof(struct data));
/* fill in the members here*/
ret->i=size;
return ret;
}
int main(void) {
struct bunchofdata *data10=data_factory(10);
data10->data_list[9].spltq=0;
printf("data10->data_list[9].spltq=%d",data10->data_list[9].spltq);
free(data10)
return 0;
}
关于c - 得到 "error: undeclared here (not in a function)"和 void * 变量数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35687137/