假设我这样分配了一个数组:
char* array[]={"This"};
然后我想给数组[ ]分配一个新值,以便它存储"This"和"That",有没有办法改变数组的大小,以便它可以容纳新的值?
假设我这样分配了一个数组:
char* array[]={"This"};
然后我想给数组[ ]分配一个新值,以便它存储"This"和"That",有没有办法改变数组的大小,以便它可以容纳新的值?
不,你不能改变数组的大小。你可以使用动态分配的 char*
列表,根据需要使用 realloc()
:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
int main()
{
char** array = malloc(1 * sizeof(*array));
if (array)
{
array[0] = "This";
printf("%s\n------\n", array[0]);
char** tmp = realloc(array, 2 * sizeof(*array));
if (tmp)
{
array = tmp;
array[1] = "That";
printf("%s\n", array[0]);
printf("%s\n", array[1]);
}
free(array);
}
return 0;
}
查看在线演示:https://ideone.com/ng00k。
无法调整数组大小。您可以简单地创建一个大小为2的新数组,然后将所有数据从先前的数组复制到新数组中。使用动态内存的realloc
可以为您完成此操作。更好的方法是使用数据结构,例如LinkedLists
或Vectors
,您可以在网上找到更多信息。
你无法调整数组对象的大小。
你需要动态分配array
的内存,并使用realloc
进行扩展。例如:
size_t current_size = 0;
char **array = malloc((current_size + 1) * sizeof *array);
if (array)
{
array[current_size++] = "This";
}
...
/**
* If realloc cannot extend the buffer, it will return NULL and leave
* the original buffer intact; however, if we assign NULL back to array,
* we lose our handle to the original buffer, causing a memory leak, so
* we assign the result to a temporary variable.
*/
char **tmp = realloc(array, (current_size + 1) * sizeof *array)
if (tmp)
{
array = tmp;
array[current_size++] = "That";
}
else
{
// realloc failed to extend the buffer; original buffer
// is left intact.
}
注意事项:
realloc
是一个相对昂贵的调用,因此你(通常)不希望像这里一样每次只扩展一个元素。更常见的策略是选择一个初始大小,涵盖大多数情况,如果需要扩展缓冲区,则将其大小加倍。
您可以将resize操作抽象为单独的函数,像这样:
int addItem(char ***arr, char *newElement, size_t *count, size_t *bufSize)
{
if (*count == *bufSize)
{
// we've run out of room; extend the buffer
char **tmp = realloc(**arr, 2 * *bufSize * sizeof **arr);
if (tmp)
{
*arr = tmp;
*bufSize *= 2;
}
else
{
// could not extend the buffer; return failure code
return 0;
}
}
(*arr)[(*count)++] = newElement;
}
并将其称为
#define N ... // initial array size
char **array = malloc(N * sizeof *array);
size_t bufSize = N;
size_t count = 0;
...
if (addItem(&array, "This", &count, &bufSize))
printf("# elements = %zu, buffer size = %zu\n", count, bufSize);
if (addItem(&array, "That", &count, &bufSize))
printf("# elements = %zu, buffer size = %zu\n", count, bufSize);
以下内容均未经测试,仅为个人想法,不提供任何明示或暗示的保证。但应该足以指引您朝着正确的方向前进。
char **array = calloc(2, sizeof(char *));
array[0] = "This";
array[1] = "That";