sách gpt4 ăn đã đi

C 语言 -> 将段落中的单词分开

In lại Tác giả: Walker 123 更新时间:2023-11-30 16:38:28 26 4
mua khóa gpt4 giày nike

我的函数 save_words 接收 Armazena 和大小。 Armazena 是一个包含段落的动态数组,size 是数组的大小。在这个函数中,我想将单词放入其他称为单词的动态数组中。当我运行它时,它崩溃了。我感谢您的帮助。

char **save_words(char **armazena, int *size)
{
char *token = NULL;
char** armazena_aux = armazena;
int i, count=0;
char **words = (char**) malloc(sizeof(char*)*(10));
for(i=0; i
{
token = strtok(*(armazena+i)," .?!,");
while( token != NULL )
{
int tam = strlen(token);
armazena[count] = (char*) malloc(tam+2);
strcpy(armazena[count],token);
armazena[count][tam+1]='\0';
count++;
token = strtok(NULL, " .?!,");

if (count%10==0)
{
words = realloc(words, sizeof(char*)*(count + 10));
}
}
}
return words;
}

câu trả lời hay nhất

armazena[count] = (char*) malloc(tam+2); 是你想要的吗?我本以为words[count] = ...;。第一次通过外循环是可以的,因为你将 armazena[0] 提升到 strtok 中,但如果它包含多个单词,第二次通过外循环将处理第一次生成的字符串。更糟糕的是,如果第一个字符串包含的单词数量超过了 armazena vector 所能容纳的数量,那么您将破坏某些内容......

关于C 语言 -> 将段落中的单词分开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47474024/

26 4 0
Walker 123
Hồ sơ

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá taxi Didi miễn phí
Phiếu giảm giá taxi Didi
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress