c-如何创建包含字符串的可变大小数组



我在创建包含字符串的可变大小数组时遇到问题。我试过创建一个多维数组,但太难了。示例:

char *audio_types[8][40];           // 8 is number of string elements in the array by default; 40 is the maximum length of a string
audio_types = (char *) malloc(15);  // increasing number of strings in the array
free(audio_types);

此外,我还试图创建可变大小的指针数组。示例:

char *audio_types[40];              // 40 is the maximum length of a string
*audio_types = (char *) malloc(8);  // setting number of strings to 8
free(audio_types);

问题是我不知道如何正确地创建一个具有可变数量字符串元素的数组。对不起,我是C编程的新手。简而言之,我的代码必须在一个数组中包含多个字符串元素。示例:

audio_types[0]  // some string...
audio_types[1]  // another string...
audio_types[2]  // more another sting... etc.

希望你能理解我想问的问题。感谢您的关注。

也许是这样的。当您添加新类型时,它将增大数组的大小。它还将跟踪数组中有多少类型(如果您动态调整数组大小,并且需要知道这个数组中有几个元素,则需要它(。

#define AUDIO_TYPE_LENGTH 40
typedef char audio_type[AUDIO_TYPE_LENGTH];
typedef struct
{
size_t size;
audio_type audio_types[];
}audioTypes_t;
audioTypes_t *AddType(audioTypes_t *types, const char *type)
{
size_t size = types ? types -> size : 0;
types = realloc(types, sizeof(*types) + (size + 1) * sizeof(types -> audio_types[0]));
if(types)
{
strncpy(types -> audio_types[size], type, sizeof(types -> audio_types[0] - 1));
types -> audio_types[size][sizeof(types -> audio_types[0] - 1)] = 0; //making strncpy safe
types -> size = size + 1;
}
return types;
}

您不能更改C中数组的大小。每次您想调整大小并将内容从旧数组转移到新数组时,都可以创建一个新数组。或者,您可以使用列表来存储字符串。

在C中定义数组(char *audio_types[40];行(时,它会为数组中的40个字符指针创建一个连续的内存块。由于内存是在定义数组时分配的,因此不能使数组变长。要做到这一点,您需要一个List数据类型,或者当您想添加一个新元素时,您可以创建一个新数组,该新元素是末尾有新元素的旧元素。

char string[40]是一个由40个字符组成的数组,最多可容纳39个字符(加上字符串终止符(-最大大小无法更改,并且即使存储包含2个字符的短字符串(加上字符终止符(,也会使用40字节的内存。

char *string是指向某些字符的指针-指针可以指向任何位置:指向单个字符或任何大小的字符数组。如果它指向一个字符数组(可以使用malloc((生成(,那么它可以被认为是一个";字符串"(

CCD_ 4是一个8〃的阵列;字符串";(实际上是8个指向字符的指针(数组的大小不能更改,但指针可以指向任何地方:指向单个字符或任何大小的字符数组。如果指针指向一个字符数组(可以使用malloc((生成(,那么它们可以被认为是";字符串"(

CCD_ 5是一个2D阵列;字符串"-其中";字符串";定义为最多可容纳39个字符(加上字符串终止符(的40个字符的数组。不能更改任一维度的大小。

CCD_ 6是指向CCD_;字符串";或者连接到";字符串";。大小不是固定的,可以动态创建。

char *string1 = "asd";意味着字符串1指向只读内存中的一个由4个字符组成的数组("asd"加上字符串终止符(-您不能更改字符串的内容,但可以将字符串1指向任何您想要的位置。

char string1[] = "asd";意味着string1是一个由4个字符组成的数组,编译器用4个字符填充("asd"加上字符串终止符(-您可以更改数组的内容,但不能更改大小。

因此,如果你想要一个动态大小的字符串数组,你可以这样做:

#include <stdio.h>  // printf
#include <stdlib.h> // malloc, realloc, free
#include <string.h> // strcpy, strdup
int main()
{
// make an array of 8 string pointers
int original_size = 8;
char **array = malloc(original_size * sizeof(char *));

// point each pointer to a string
// you don't have to do this all at once if you don't want to
for (int i=0; i<original_size; i++)
array[i] = malloc(40 * sizeof(char)); // max of 39 plus the terminator

// array[7] is already allocated so we can just use it as a string
strcpy(array[7],"asd"); // the last string that we malloced

// reallocate the array to be bigger
int new_size = 15;
array = realloc(array, new_size * sizeof(char *));
// array[14] is a pointer that doesn't point anywhere
// so we need to allocate space before filling it in
array[14] = strdup("qwe"); // points 3 characters plus the string terminator

printf("%sn",array[7]);
printf("%sn",array[14]);

// free the one we made with strdup
free(array[14]);

// free the 8 we malloced
for (int i=0; i<original_size; i++)
free(array[i]);

// now free the array we malloced and then realloced
free(array);

return 0;
}

注意:在这个程序中没有错误检查,因为我不想掩盖基本的想法-确保你在制作的任何程序中都添加了错误检查

试试看https://onlinegdb.com/XQeyOGd0C

最新更新