|
#include <stdio.h>
#include <stdlib.h
#include <string.h>
int main(void)
{
int i;
int count;
char **pt;
printf("How many words do you wish to enter? \n");
scanf("%d",&count);
printf("Enter %d words now: \n",count);
pt=(char**)malloc(count*sizeof(char*));
for(i=0;i<count;i++)
{
char *wordpt;
int len;
char temp[100];
scanf("%s",temp);
len=strlen(temp);
wordpt=(char*)malloc(len*sizeof(char));
strcpy(wordpt,temp);
// *pt[ i]=wordpt;
*(pt+i)=wordpt;
// free(wordpt);
}
for(i=0;i<count;i++)
{
printf("%s\n",*(pt+i));
}
free(pt);
printf("Done!\n");
return 0;
}
1.為什么使用*pt[ i]=wordpt;會(huì)報(bào)錯(cuò),而*(pt+i)=wordpt;卻可以,不是一樣的嗎?
2.為什么wordpt不用free?free了就會(huì)報(bào)錯(cuò)。
|
|