[英]C - Dynamic array of struct error
I'm trying to implement a multithreaded C program and I'm stuck. 我正在尝试实现多线程C程序,但遇到了麻烦。 I have a struct which holds a char array and a pointer to another struct (I'm trying to use that pointer as an array of buffer_item s:
我有一个结构,其中包含一个char数组和一个指向另一个结构的指针(我正在尝试将该指针用作buffer_item的数组:
typedef struct{
char** buffer;
} buffer_item;
typedef struct{
buffer_item* buffers;
char file_name[255];
}buffer_holder;
When I try to create buffer_item s and put them into buffer_holder 's buffers array, gcc gives me the following error: 当我尝试创建buffer_item S和把它们放到buffer_holder的缓冲器阵列,海湾合作委员会给了我下面的错误:
for(i = 0; i < n_value; i++){
buffer_holder *holder = (buffer_holder*) malloc(sizeof(buffer_holder));
int l;
for(l = 0; l < r_value; l++){
buffer_item *new_item = (buffer_item *)malloc(sizeof(buffer_item));
new_item->buffer = malloc(buffer_size * sizeof(char*));
int j;
for (j = 0; j < buffer_size; j++)
new_item->buffer[j] = malloc((MAX_WORD+1) * sizeof(char));
holder -> buffers[l] = new_item;
}
strcpy(holder -> file_name, argv[i+3]);
if(pthread_create(&mapper_threads[i], NULL, mapper, (void*) holder)) {
fprintf(stderr, "Error creating thread\n");
return 1;
}
}
wcountb2.c:187:28: error: incompatible types when assigning to type 'buffer_item' from type 'struct buffer_item *' holder -> buffers[l] = new_item;
wcountb2.c:187:28:错误:从类型'struct buffer_item *'持有人分配给类型'buffer_item'时,类型不兼容-> buffers [l] = new_item;
I have been trying to solve it but could not find anything. 我一直在尝试解决它,但找不到任何东西。 Any help will be appreciated.
任何帮助将不胜感激。 Thank you all.
谢谢你们。
holder->buffers
is a pointer to struct buffer_item
, but in the assignment holder->buffers[l] = new_item
you treat it like an array of pointers. holder->buffers
是指向struct buffer_item
的指针,但是在赋值holder->buffers[l] = new_item
您将其视为指针数组。
to fix it just change holder->buffers[l] = new_item;
要解决它,只需更改
holder->buffers[l] = new_item;
into holder->buffers = new_item;
进入
holder->buffers = new_item;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.