所以用户要输入未知数量的单词,我假设每个单词的最大长度为 10;我从 realloc 中得到了作为赋值 errr 的左操作数所需的左值。我是 C 新手,我尝试了 google,但找不到有用的答案。
代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CAPACITY 10
#define NUM_OF_WORDS 10
int main(void)
{
char *word= malloc(10*sizeof(char));
char *w[NUM_OF_WORDS];
int i;
int n;
for(i = 0 ; scanf("%s", word)==1; ++i)
{
if( i == NUM_OF_WORDS-1)
w = realloc(w, (NUM_OF_WORDS*=2) * sizeof(char));
w[i] = malloc( strlen(word)+1 * sizeof(char));
strcpy(w[i], word);
}
return 0;
}