int main(void)
{
int howMany,i,j;
char* temp = NULL;
char** friends = NULL;
printf("Please enter the number of the friends you have\n");
scanf(" %d",&howMany);
howMany++;
friends = (char**) malloc(sizeof(char*)*howMany);
for (i = 0; i < howMany; i++)
{
temp = (char*) malloc(20*sizeof(char));
fgets(temp,20,stdin);
temp[strspn(temp, "\n")] = '\0';
*(friends + i) = (char*)realloc(temp,sizeof(char) * (strlen(temp)+1));
}
for (i = 0; i < howMany; i++)
{
for ( j = 0; j < strlen(*(friends+i)); j++)
{
printf("%c",friends[i][j]);
}
printf("\n");
}
for (i = 0; i < howMany; i++)
{
free(*(friends + i));
}
free(friends);
getchar();
return 0;
}
我的代码的目的是获取我拥有的朋友数量,他们的名字,最后将其打印到屏幕上,我的代码无效的任何想法?
输入: 2 丹尼尔 大卫
输出:
(\ n)的
预期产量: 丹尼尔 大卫