C 在arrayList中的特定位置插入值
因此,我尝试向arraylist添加一个特定的值。该程序可以工作,但当我检查是否添加了该值时,每次都会得到相同的数字C 在arrayList中的特定位置插入值,c,arraylist,data-structures,realloc,memmove,C,Arraylist,Data Structures,Realloc,Memmove,因此,我尝试向arraylist添加一个特定的值。该程序可以工作,但当我检查是否添加了该值时,每次都会得到相同的数字 void arrayListInsert(struct ArrayList * list, unsigned index, short value){ if(index > list->size){ printf("Index bigger than size "); return; } if(list->
void arrayListInsert(struct ArrayList * list, unsigned index, short value){
if(index > list->size){
printf("Index bigger than size ");
return;
}
if(list->size == list->capacity){
short *array=(short *)realloc(list->array,2*sizeof(list->capacity));
list->capacity = list->capacity * 2;
memmove(&array[index],&list->array[list->size-1],sizeof(short *));
array[index]=value;
list->array=array;
list->size++;
}
}
通常不要将
malloc()
和void*
的返回值强制转换为任何其他指针类型。另外,始终根据NULL
验证malloc()
的返回值。为什么要执行sizeof(list->capacity)
?您需要使用类似于list->capacity*sizeof(short)
的功能。目前,2*sizeof(list->capacity)
是一个常量表达式。它没有意义,也不起作用。通常不要将malloc()
和void*
的返回值强制转换为任何其他指针类型。另外,始终根据NULL
验证malloc()
的返回值。为什么要执行sizeof(list->capacity)
?您需要使用类似于list->capacity*sizeof(short)
的功能。目前,2*sizeof(list->capacity)
是一个常量表达式。这毫无意义,也不起作用。