首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

链表

2012-06-09 
链表求助#includestdio.h#includestdlib.hstruct stu{int numchar name[12]int age[0]struct stu *

链表求助
#include<stdio.h> 
#include<stdlib.h> 
struct stu{ 
  int num; 
  char name[12]; 
  int age[0]; 
  struct stu *next; 
  }; 

int main(){ 

struct stu *curr,*head; 

head=(stu *)malloc(sizeof(stu)); 
curr=head; 
scanf("%d",&(head->num)); 
curr->next=NULL; 

while(curr->num!=0){ 
  scanf("%s%d",curr->name,curr->age); 
  curr=(stu *)malloc(sizeof(struct stu)); 
  scanf("%d",&(curr->num)); 
  curr->next=NULL; 

return 0; 


-------------------------------------- 


输入:
101 zhang 19 

内存信息:
http://flic.kr/p/ccMQey 

why 0x6f17e0 change from 00-->13 unexpectedly. 
I think the "*next" and "int age"storaged in different place. 
but it looks like it overwrite? 


[解决办法]

C/C++ code
#include<stdio.h>  #include<stdlib.h>  typedef struct stu{      int num;  //    char name[12];  //    int age;      struct stu *next;  }student;  int main(){      student *curr,*head,*temp;      int num=1;    head=(student *)malloc(sizeof(student));      temp=head;      scanf("%d",&(head->num));      while(num!=0){  //        scanf("%s%d",curr->name,curr->age);          curr=(student *)malloc(sizeof(student));          scanf("%d",&num);          curr->num=num;        temp->next=curr;        temp=curr;        curr->next=NULL;    }    temp->next=NULL;    while(head->next!=NULL)    {        printf("%d\n", head->num);        head=head->next;    }    return 0;  }
[解决办法]
1楼有问题,楼主本身age[0];有问题。。。。

热点排行