linux下bug
《------------------------------------User.c--------------------------------------》程序代码:
#include<stdio.h> #include<stdlib.h> #define USER_NAME 20 typedef struct User{ struct User *link; char name[USER_NAME]; int age; } user;
《------------------------------------test.c--------------------------------------》
程序代码:
#include<stdio.h> #include"User.c" user *start; user *this; user *previous; user *stop; int add(){ int i,j,c; printf("printf user number:"); scanf("%d",&j); for(i=1;i<=j;i++){ if(i==1){ this = start = previous = (user *)malloc(sizeof(this)); }else{ this = (user *)malloc(sizeof(this)); previous->link = this; previous = this; }; printf("printf user %d name:",i); getchar(); gets(this->name); printf("printf %s age:",this->name); scanf("%d",&(this->age)); printf("printf age is %d\n",this->age); }; stop = this; } int printfs(){ this = start; while(this!=NULL){ printf("%s ::::::: %d\n",this->name,this->age); this = this->link; } } int frees(){ while(start!=NULL){ free(start); start = start->link; } } int main(){ add(); printfs(); printfs(); frees(); }
《------------------------------------运行结果--------------------------------------》
xujia@xujia-Rev-1-0:~/C/project_one$ ./one
printf user number:4
printf user 1 name:name1
printf name1 age:1
printf age is 1
printf user 2 name:name2
printf name2 age:2
printf age is 2
printf user 3 name:name3
printf name3 age:3
printf age is 3
printf user 4 name:name4
printf name4 age:4
printf age is 4
name1 ::::::: 50
name2 ::::::: 51
name3 ::::::: 52
name4 ::::::: 4
name1 ::::::: 50
name2 ::::::: 51
name3 ::::::: 52
name4 ::::::: 4
xujia@xujia-Rev-1-0:~/C/project_one$ ./one
printf user number:4
printf user 1 name:n1
printf n1 age:1
printf age is 1
printf user 2 name:n2
printf n2 age:2
printf age is 2
printf user 3 name:n3
printf n3 age:3
printf age is 3
printf user 4 name:n4
printf n4 age:4
printf age is 4
n1 ::::::: 1
n2 ::::::: 2
n3 ::::::: 3
n4 ::::::: 4
n1 ::::::: 1
n2 ::::::: 2
n3 ::::::: 3
n4 ::::::: 4