空指针的动态数组 [英] Dynamic Array of Void Pointers
问题描述
我正在尝试基于动态数组创建动态集抽象数据类型.但是,当我尝试将数据添加到数组时,出现编译器警告和错误:
I'm trying to create a dynamic set abstract data type, based on a dynamic array. However, I get a compiler warning and an error when I try to add the data to the array, which are:
警告:取消引用"void *"指针(默认情况下启用)
warning: dereferencing 'void *' pointer [enabled by default]
错误:无效使用void表达式
error: invalid use of void expression
我的代码如下,我用注释标记了有问题的行
My code is as follows, I've marked the problematic line with a comment
struct SET
{
//general dynamic array
void *data;
int elements; //number of elements
int allocated; // size of array
};
struct SET create()
{
//create a new empty set
struct SET s;
s.data = NULL;
s.elements = 0;
s.allocated = 0; //allocations will be made when items are added to the set
puts("Set created\n");
return s;
}
struct SET add(struct SET s, void *item)
{
//add item to set s
if(is_element_of(item, s) == 0) //only do this if element is not in set
{
if(s.elements == s.allocated) //check whether the array needs to be expanded
{
s.allocated = 1 + (s.allocated * 2); //if out of space, double allocations
void *temp = realloc(s.data, (s.allocated * sizeof(s))); //reallocate memory according to size of the set
if(!temp) //if temp is null
{
fprintf(stderr, "ERROR: Couldn't realloc memory!\n");
return s;
}
s.data = temp;
}
s.data[s.elements] = item; //the error is here
s.elements = s.elements + 1;
puts("Item added to set\n");
return s;
}
else
{
fprintf(stdout, "Element is already in set, not added\n");
return s;
}
}
我已经对void指针进行了研究,但显然我在这里缺少了一些东西.我将不胜感激.感谢您阅读并希望回答!
I've done research on void pointers, but clearly I'm missing something here. I'd appreciate any help I can get. Thanks for reading and hopefully answering!
推荐答案
首先,我认为您打算在结构中包含的是通用指针数组(void *
数组),因为您的项是void *
,并且您想将它们存储为数组.也就是说,您需要一个void *
的动态数组,因此,您应该使用void **
:
First, I think what you intend to have in your structure is an array of generic pointers (array of void *
), because your items are void *
, and you want to store them as an array. That is, you want a dynamic array of void *
, thus, you should use void **
:
struct SET {
void **data;
int elements;
int allocated;
};
当然,您的add
函数需要更新:
Of course, your add
function needs to be updated:
struct SET add(struct SET s, void *item) {
if (is_element_of(item, s) == 0) {
if (s.elements == s.allocated) {
s.allocated = 1 + (s.allocated * 2);
void **temp = realloc(s.data, (s.allocated * sizeof(*s.data)));
if (!temp) {
fprintf(stderr, "ERROR: Couldn't realloc memory!\n");
return s;
}
s.data = temp;
}
s.data[s.elements] = item;
s.elements = s.elements + 1;
puts("Item added to set\n");
return s;
}
else {
fprintf(stdout, "Element is already in set, not added\n");
return s;
}
}
请注意,realloc
行已更改:您不想重新分配给s.allocated * sizeof(s)
,而是想要s.allocated*sizeof(*s.data)
,因为您将存储void *
类型的元素(*s.data
的类型为void *
,我没有明确编写void *
以便更容易适应将来的更改.
Note the realloc
line was changed: you don't want to realloc to s.allocated * sizeof(s)
, you want s.allocated*sizeof(*s.data)
, since you'll be storing elements of type void *
(the type of *s.data
is void *
, I didn't explicitly write void *
to make it easier to accomodate possible future changes).
此外,我相信您应该更改函数以接收和返回指向struct SET
的指针,否则,您将始终在结构周围进行复制(请记住,值是通过复制传递的.)
Also, I believe you should change your functions to receive and return pointers to struct SET
, otherwise, you will always be copying around the structure (remember that values are passed by copy).
这篇关于空指针的动态数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!