C:删除数组

C:删除数组,c,C,我是c语言的新手。我想创建一个数组,然后删除它,然后将另一个数组放入其中。我该怎么做呢?如果您正在寻找C语言中的动态数组,它们相当简单 1) 声明跟踪内存的指针, 2) 分配内存, 3) 使用内存, 4) 释放内存。 int *ary; //declare the array pointer int size = 20; //lets make it a size of 20 (20 slots) //allocate the memory for the array ary = (int*)

我是c语言的新手。我想创建一个数组,然后删除它,然后将另一个数组放入其中。我该怎么做呢?

如果您正在寻找C语言中的动态数组,它们相当简单

1) 声明跟踪内存的指针,
2) 分配内存,
3) 使用内存,
4) 释放内存。

int *ary; //declare the array pointer
int size = 20; //lets make it a size of 20 (20 slots)

//allocate the memory for the array
ary = (int*)calloc(size, sizeof(int));

//use the array
ary[0] = 5;
ary[1] = 10;
//...etc..
ary[19] = 500;

//free the memory associated with the dynamic array
free(ary);

//and you can re allocate some more memory and do it again
//maybe this time double the size?
ary = (int*)calloc(size * 2, sizeof(int));

可以找到有关
calloc()
的信息,同样的事情也可以通过
malloc()
实现,而不是使用
malloc(size*sizeof(int))

听起来像是在询问是否可以重复使用指针变量在不同时间指向不同的堆分配区域。是的,你可以:

void *p;         /* only using void* for illustration */

p = malloc(...); /* allocate first array */
...              /* use the array here   */
free(p);         /* free the first array */

p = malloc(...); /* allocate the second array */
...              /* use the second array here */
free(p);         /* free the second array */

您能告诉我们您是如何分配阵列的吗?如果您使用malloc,您可以使用free来取消分配它,然后分配一个新的。如果您使用malloc,您就有了代码。如果你有代码,你应该发布它。