gpt4 book ai didi

c - malloc() 用于字符串数组,但没有像希望的那样工作

转载 作者:行者123 更新时间:2023-11-30 16:34:05 25 4
gpt4 key购买 nike

我正在尝试创建一个函数,它接受一个字符串和一个指向字符串数组的指针以及malloc() char数组的数组,并复制字符串的每个单独的单词。这就是我到目前为止所拥有的,我想我已经接近了,我只是在努力在数组数组上使用 malloc()

int string_parser(char *inp, char **array_of_words_p[])
{
int CurrentChar = 0; //Variable Initialization
char *buffer; //Variable Initialization

/* Allocate memory and check for errors allocating memory */
//Allocate memory to buffer the size of the input string
buffer = (char*)malloc(strlen(inp));
if (buffer == NULL)
{
printf("Error allocating memory..\n");
return -1;
}

/* Move input string into buffer before processing */
for (CurrentChar = 0; CurrentChar < strlen(inp) + 1; CurrentChar++)
{ //For every character in input
if (inp != NULL)
{
//Move input character into buffer
buffer[CurrentChar] = inp[CurrentChar];
}
}

/* Convert string into array of words */
char ** stringbuffer = NULL;

//Convert string to array of words
char * CurrentWord = strtok_s(buffer, " ", *array_of_words_p);

//Variable Initialization
int numspaces = 0;

while (CurrentWord)
{
//Allocate memory for size of string
stringbuffer = (char**)realloc(stringbuffer, sizeof(char**) * ++numspaces);
if (stringbuffer == NULL)
{
return -1;
}
stringbuffer[numspaces - 1] = CurrentWord;

//Reset Current word to null
CurrentWord = strtok_s(NULL, " ", *array_of_words_p);
}

//Reallocate memory to include terminating character
stringbuffer = (char**)realloc(stringbuffer, sizeof(char**) * (numspaces + 1));
stringbuffer[numspaces] = 0;

/* Write processed data into returned argument */
*array_of_words_p = (char**)malloc(sizeof(char**) * (numspaces + 2));
memcpy(*array_of_words_p, stringbuffer, (sizeof(char*) * (numspaces + 2)));

free(stringbuffer);
return numspaces;
}

最佳答案

    //Allocate memory to buffer the size of the input string
buffer = (char*)malloc(strlen(inp));

输入字符串的大小包括终止\0 ,所以你需要:

    buffer = malloc(strlen(inp)+1);
    //Convert string to array of words
char * CurrentWord = strtok_s(buffer, " ", *array_of_words_p);

滥用 *array_of_words_p 是不明智的对于上下文保存变量,因为这需要对其进行适当的初始化。更好:

    char *context, *CurrentWord = strtok_s(buffer, " ", &context);

CurrentWord = strtok_s(NULL, " ", &context);
        //Allocate memory for size of string
stringbuffer = (char**)realloc(stringbuffer, sizeof(char**) * ++numspaces);

这可能不会造成伤害(由于指针大小相同),但是 sizeof(char**)严格来说是错误的,因为字符串数组的元素类型为 char * 。正确:

        stringbuffer = realloc(stringbuffer, sizeof (char *) * ++numspaces);

stringbuffer = realloc(stringbuffer, sizeof (char *) * (numspaces + 1));
    /* Write processed data into returned argument */
*array_of_words_p = (char**)malloc(sizeof(char**) * (numspaces + 2));
memcpy(*array_of_words_p, stringbuffer, (sizeof(char*) * (numspaces + 2)));

free(stringbuffer);

您可以避免这种不必要的复制,并避免访问未分配的内存 stringbuffer[numspaces+1]将上面的内容替换为:

    *array_of_words_p = stringbuffer;

除此之外,您的函数可以正常工作并且可以像这样调用:

    char **array_of_words;
int n = string_parser("this here is an example string", &array_of_words);
for (int i = 0; i < n; ++i) puts(array_of_words[i]);

关于c - malloc() 用于字符串数组,但没有像希望的那样工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49425319/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com