为什么要 定义一个字符数组 再转长整型
程序代码:
#include<stdio.h> #include<stdlib.h> struct stud { long num; char name[20]; struct stud * next; }; void new_stud(); void listall(); struct stud *head,*cur,*newp; int main(char argc,char** argv ) { char ch; int isClose=1; head=NULL; while(isClose) { printf("\n type 'E' or 'e' to enter new student,"); printf("\n type 'L' or 'l' to list all students:"); ch=getchar(); getchar(); switch(ch) { case 'e': case 'E':new_stud();break; case 'l': case 'L':listall();break; default:isClose=0; }; } return 0; } void new_stud() { char numstr[20]; newp=(struct stud *)malloc(sizeof(struct stud)); if(head==NULL) { head=newp; } else { while(cur->next!=NULL) { cur=cur->next; } cur->next=newp; } cur=newp; printf("\n enter NO.:"); gets(numstr); /*这里输入学号为什么不能 gets(cur->num) ?~ 而要定义一个字符数组再转成长整型 ?~*/ cur->num=atol(numstr); printf("\n enter name:"); gets(cur->name); cur->next=NULL; } void listall() { int i=0; if(head==NULL) { printf("\n No Student!"); return; } cur=head; for(;cur!=NULL;) { printf("\n============================="); printf("\n Num: %ld",cur->num); printf("\n Name: %s",cur->name); printf("\n------------------------"); cur=cur->next; } }