读书人

链表

发布时间: 2012-06-09 17:16:42 作者: rapoo

链表求助
#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];有问题。。。。

读书人网 >C语言

热点推荐