我正在编写一个程序,它应该通过在一个名为GetInput的函数中使用输入重定向从文本文件中获取其输入.(文本文件包含10个单词.)然后代码应该能够在Print函数中打印ListWord的内容.
这就是我到目前为止所拥有的.
我在尝试运行此代码时一直遇到错误.我尝试在ListWord之前删除*并且代码有效,但它不保留存储在其中的单词(字符串).但是在ListWord之前移除*对我来说没有意义.我究竟做错了什么?
void GetInput( char** ListWord)
{
int i=0;
char word[30]; //each word may contain 30 letters
*ListWord = malloc(sizeof(char*)*10); //there are 10 words that needs to be allocated
while(scanf("%s", word)==1) //Get Input from file redirection
{
*ListWord[i]= (char *)malloc(30+1);
printf("%s\n", word); //for checking
strcpy(*ListWord[i], word);
printf("%s\n", *ListWord[i]); //for checking
i++;
}
}
void Print(char *ListWord)
{
//print ListWord
int i;
for (i=0; i<10; i++)
{
printf("%s", ListWord[i]);
}
}
int main()
{
char * ListWord;
GetInput(&ListWord);
printf("%s\n", ListWord[0]); …Run Code Online (Sandbox Code Playgroud)