在C中的动态数组中插入元素的问题

问题描述 投票:0回答:1

我有一个带有此结构的动态数组:

typedef struct vector_struct {
   size_t e_sz;
   char e_type;

   #define V_INT 1
   #define V_DOUBLE 2
   #define V_CHAR 3
   #define V_FLOAT 4


unsigned no_e;
unsigned cur_cap;
void* e_array;

}* vector_type;

其中no_e是大小,cur_cap是容量,e_sz是数组中元素的大小,e_array是空指针。

我必须完成一个push_back函数,该函数必须适用于上面定义的4个差分类型。

void v_push_back(vector_type v, void* new_val){

if( v->no_e >= v->cur_cap ){

    /*** reallocate a larger array ***/
    v->cur_cap += (v->cur_cap) ? v->cur_cap : 2;

    v->e_array = realloc(v->e_array, v->cur_cap*(v->e_sz))

}
/*** copy new_val in the array at index v->no_e ***/

/*** TO BE DONE START ***/

if(v->e_type == 1)
    memcpy(((int*)v->e_array) + v->no_e*(v->e_sz), new_val, v->e_sz);
else if(v->e_type == 2)
    memcpy(((double*)v->e_array) + v->no_e*(v->e_sz), new_val, v->e_sz);
else if(v->e_type == 3)
    memcpy(((char*)v->e_array) + v->no_e*(v->e_sz), new_val, v->e_sz);
else if(v->e_type == 4)
    memcpy(((float*)v->e_array) + v->no_e*(v->e_sz), new_val, v->e_sz);

/*** TO BE DONE END ***/

(v->no_e)++;
} 

此函数适用于char,但似乎未插入int或double。我在代码中找不到错误。

c dynamic-arrays push-back
1个回答
0
投票

... + v->no_e*(v->e_sz)是错误的。您已经强制转换为适当的类型,因此,如果对项目数组执行+,则将获得该特定类型的指针算术。只需执行... + v->no_e就足够了,因为项目大小基于所指类型隐式存在。这就是为什么1个字节的字符可以工作,而没有其他原因的原因。

指针算术而不是数组索引通常很难阅读。我会像这样重写代码:

void* item;
switch(v->e_type)
{
   case V_INT:     item = &( (int*)    v->e_array )[v->no_e]; break;
   case V_DOUBLE:  item = &( (double*) v->e_array )[v->no_e]; break;
   case V_CHAR:    item = &( (char*)   v->e_array )[v->no_e]; break;
   case V_FLOAT:   item = &( (float*)  v->e_array )[v->no_e]; break;
}
memcpy(item, new_val, v->e_sz);

请注意,这里和那里的几个空格如何使重复代码更具可读性,并最大程度地减少了与拼写错误有关的错误。另请注意,此代码利用了[]运算符优先级高于&。

的优势。
© www.soinside.com 2019 - 2024. All rights reserved.