【发布时间】:2013-04-18 20:02:31
【问题描述】:
由于某种原因,我无法正常工作。这是我在代码中的功能,如果这还不够帮助,我会很乐意发布整个代码。
char addcity()
{
citylist=malloc(sizeof(struct city));
struct city *temp;
char addcity[50];
char addstate[50];
char addpopulation[50];
char addregion[50];
char addzipcode[50];
//temp = citylist;
//citylist=malloc(sizeof(struct city));
//struct city *ptr=citylist;
struct city *ptr= (struct city*)malloc(sizeof(struct city));
printf("Which city would you like to add?: ");
scanf("%s",addcity);
printf("Which state is the city in?: ");
scanf("%s",addstate);
printf("What is the population?: ");
scanf("%s", addpopulation);
printf("what is the region?: ");
scanf("%s", addregion);
printf("what is the zipcode?: ");
scanf("%s", addzipcode);
int found;
ptr->name=addcity;
ptr->statecode=addstate;
ptr->population=addpopulation;
ptr->region=addregion;
ptr->zipcode=addzipcode;
ptr->next=NULL;
curr->next=ptr;
curr=ptr;
printlist(); //this just prints out the whole list.
有时我得到一个分段错误,而其他时候它根本不起作用。我必须使用 strcpy(a,b) 来复制字符串吗?
【问题讨论】:
-
请不要张贴未注释的代码墙并要求我们为您调试。设身处地为我们着想——你会帮助提出这样问题的人吗?请证明您至少已经做出了自己调试的基本努力。给我们调试器、程序等的输出。最后,给我们一段代码,我们可以自己编译以重现错误。否则,我们无法知道该错误不在其他地方。
-
也许你需要阅读指针pw1.netcom.com/~tjensen/ptr/ch1x.htm
-
在调试器中单步执行可能会有所帮助。此外,理解指针和 C 范围也可能有所帮助。
标签: c string pointers linked-list