条件while(scanf("%s", str) != EOF) 是一个潜在的无限循环,只有在您通过命令行向程序输入EOF 信号时才会停止。
对于包含所有单词的单个字符串,您需要在每次读取时重新分配内存以增加保存字符串的内存空间。
你可以这样做:
Live demo
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int ret_words(char **words) { //double pointer argument
char str[50];
char *temp = malloc(1); //first allocation required
int count = 0;
if(temp == NULL){ //allocation error
return -1;
}
do{
temp = realloc(temp, strlen(str) + strlen(temp) + 2); //reallocate space on
//each iteration
if(temp == NULL){
return -1;
}
count++; //count words
} while (scanf("%49s", str) == 1 && strcat(temp, str) //scan str and append to temp
&& getchar() != '\n' && strcat(temp, " ")); //with spaces between
*words = temp; //assign temp to words
return count; //return word count
}
int main() {
int ret;
char *words; //pointer to the array of strings
puts("Enter the string");
putchar('>');
ret = ret_words(&words); //returned number of words on success, -1 on error
printf("Inputed string: %s\nNumber of words: %d", words, ret); //test print
}
约束:在最后一个输入的单词之后不能有任何空格来结束循环。
现在,如果您想要二维数组结构,不仅为单词本身动态分配,还为指向这些字符串的指针动态分配,您将需要一些更特殊的东西,一个三重指针。
将其视为指向字符串数组的指针,这是您需要作为函数参数的内容,以便使调用函数(在本例中为 main 函数)中的更改永久化。
Live demo
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int ret_words(char ***words) { //triple pointer argument
char str[50];
int count = 0;
char **temp = malloc(sizeof *temp); //allocate a pointer to pointer to char
if(temp == NULL){ //allocation error
return -1;
}
do {
temp = realloc(temp, (count + 1) * sizeof *temp); //reallocate space at every scan
if(temp == NULL){
return -1;
}
temp[count] = malloc(strlen(str) + 1);
count++;
} while (scanf("%49s",temp[count - 1]) == 1 //scan str and append to temp
&& getchar() != '\n'); //with spaces between
*words = temp; //assing the allocated array of pointer to the argument
return count; //return word count
}
int main()
{
char **words; // pointer to pointer to navigate the array of strings
int ret;
puts("Enter the string");
putchar('>');
ret = ret_words(&words); //returned number of words on success, -1 on error
for(int i = 0; i < ret; i++) //print each stored one word string
printf("Word %d: %s\n",i + 1, words[i]);
printf("Word count: %d", ret); //print the number of words
}
约束:在最后一个输入的单词之后不能有任何空格来结束循环。
请注意,由于这些分配是通过函数的参数进行的,因此存在额外的间接级别,如果它们要在适当的位置使用或由函数返回,您只需要一个简单的指针,在第一个样本,第二个样本中有一个双指针。