我对结构指针数组的增长有疑问。
指向结构存储内存位置的指针数组。
但我不确定我要存储多少。
我想要数组的动态增长。
我还可能需要删除其中一个元素。
代码如下:</p>
#include <stdio.h>
#include <stdlib.h>
struct dominate * realloct(int* dim_1, struct dominate *dominateList)
{
int i;
struct dominate *dominateList_temp = (struct dominate *)malloc(sizeof(struct dominate *)*(*dim_1+10));
for(i = 0; i < *dim_1+10;i++)
{
if(i<*dim_1)
{
dominateList_temp[i] = dominateList[i];
//memcpy(b[i], a[i], (long)(sizeof(int)*(*dim_1)));
}
else
dominateList_temp[i] = (struct dominate *)malloc(sizeof(struct dominate *)*dim_1);
}
(*dim_1) = (*dim_1)+10;
return dominateList_temp;
}
struct dominate
{
double ID;
};
struct dominate *head;
int main()
{
int i;
struct dominate *dominateList;
struct dominate *dominateList_temp;
int dim_1 = 10;
struct dominate *z[100];
for(i = 0; i < 100; i++){
head = (struct dominate *) malloc(sizeof(struct dominate *));
head->ID = i;
z[i] = head;
}
dominateList = (struct dominate *)malloc(sizeof(struct dominate *)*dim_1);
for(i = 0; i < 100; i++){
if(i == dim_1 )
{
dominateList_temp = realloct(&dim_1, dominateList);
free(dominateList);
dominateList = dominateList_temp;
}
}
printf("%d \n\n", dim_1);
for(i = 0; i < 100; i++){
printf("%.2lf ", dominateList[i].ID);
printf("\n");
}
return 0;
}
我不知道如何修改这个问题
if(i<*dim_1){
dominateList_temp[i] = dominateList[i];
//memcpy(dominateList_temp[i], dominateList[i], (long)(sizeof(struct dominate *)*dim_1);
}
else
dominateList_temp[i] = (struct dominate *)malloc(sizeof(struct dominate *)*dim_1);
另外,如果我想删除数组的一个元素,该怎么做呢?