C:删除数组

Tom*_*Tom 2 c

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

Cha*_*had 5

如果您在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));
Run Code Online (Sandbox Code Playgroud)

有关信息calloc()可以在这里找到,同样的事情可以malloc()通过改为使用malloc(size * sizeof(int));