我正在用 C 语言编写一个小程序,我需要一种向量/ArrayList/LinkedList,但我正在使用 C 语言。知道如何在 C 语言中做这种事情吗?
我想存储结构,然后添加/删除一些结构。
对于可调整大小的数组,您可以使用malloc()
and realloc()
。这些允许您预订(通过malloc()
)并调整大小(与realloc()
)堆上一定量的空间。它们的使用方式如下:
int* a = malloc(10 * sizeof(int));
if(a == NULL) {} // malloc() was unable to allocate the memory, handle the
// error and DO NOT use this pointer anymore
// now you can treat a as a normal array of 10 ints:
a[4] = 51;
// suppose 10 ints aren't no more enough:
a = realloc(a, 20 * sizeof(int));
if(a == NULL) {} // same thing as before
// here you have 20 ints, the previous 10 are still there
a[18] = a[4]
// don't forget to free the memory when you have finished:
free(a);
只需将“int”替换为您的结构类型即可。 ;)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)