关于动态分配字符串的问题!
程序代码:#include <stdio.h>
#include <string.h>
#define STR_LEN 30
#define MAX_LEN 30
main()
{
char *p[MAX_LEN], str[STR_LEN+1];
int n=0, num_p=0, i, j;
for (;;)
{
if (num_p>MAX_LEN)
{
printf("-- There is no place left --");
break;
}
printf("Enter the sentence: ");
scanf(" %s",&str);
if (strcmp(str,"0")==0)
break;
while (getchar()!='\n')
;
p[n]=malloc(strlen(str)+1);
if (p[n]==NULL)
{
printf("-- malloc error --");
break;
}
strcpy(p[n++],str);
num_p++;
}
for (i=0;i<n;i++)
printf("%s\n",p[n]);
getch();
return 0;
}
运行后屏幕上都是null不知道具体是什么原因
望解答










