读书人

这段简单的代码哪儿出有关问题了

发布时间: 2012-09-12 09:21:30 作者: rapoo

这段简单的代码,哪儿出问题了?

C/C++ code
#include<stdio.h>#include<stdlib.h>struct stu{    char name[20];    int no;    char sex;    int age;    struct stu* next;};struct stu* createList(int n){    if(!n)return NULL;    struct stu * head=NULL,* tail=NULL,* cur=NULL;    printf("input name,number,sex,age:\n");    int i=0;    for(;i<n;++i){        cur=(struct stu*)malloc(sizeof(struct stu));        cur->next=NULL;        scanf("%s%d%c%d",cur->name,&(cur->no),&(cur->sex),&(cur->age));        if(tail==NULL && head==NULL){            tail=head=cur;        }        else{            tail->next=cur;            tail=cur;        }    }    return head;}void displayList(struct stu* head){    if(!head)return;    printf("name \t number \t sex \t age \n");    struct stu* p=head;    while(p){        printf("%s \t %d \t %c \t %d \n",p->name,p->no,p->sex,p->age);        p=p->next;    }}int main(){    int n=1;    struct stu* head = createList(n);    displayList(head);    return 0;}


[解决办法]
格式化那边改为

scanf("%s%d%1s%d",cur->name,&(cur->no),&(cur->sex),&(cur->age));

读书人网 >C语言

热点推荐