请教各位编程大哥大姐,我有一个共用体的问题。。。

来源:百度知道 编辑:UC知道 时间:2024/05/19 12:47:37
#define NULL 0
#include "stdio.h"
#include "malloc.h"
#define LEN sizeof(struct student)
struct student
{
long int num;
char name[20];
char sex;
char job;
union
{
int classes;
char position[20];
}category;
struct student *next;
};
int n;
struct student *creat(void)
{
struct student *p1,*p2,*head;
n=0;
p1=(struct student *)malloc(LEN);
printf("please enter the data of student:");
scanf("%ld%s%c%c",&p1->num,p1->name,&p1->sex,&p1->job);
if(p1->job=='s') scanf("%d",&p1->category.classes);
else if(p1->job=='t') scanf("%s",p1->category.position);
else printf("input error!\n");
head=NULL;
while(p1->num!=NULL)
{
n=n+1;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(stru

创建过程有些小毛病。修改如下:

struct student *creat(void)
{
struct student *p1,*p2,*head;
n=0;
p1=p2=(struct student *)malloc(LEN);
printf("please enter the data of student(sex,job,num,name):\n");
scanf("%c%c%ld%s",&p1->sex,&p1->job,&p1->num,p1->name);
if(p1->job=='s') scanf("%d",&p1->category.classes);
else if(p1->job=='t') scanf("%s",p1->category.position);
else {printf("input error!\n");return NULL;}
head=NULL;

while(p1->num!=NULL)
{
n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
printf("input new record:(sex,job,num,name)\n");
scanf("%c%c%ld%s",&p1->sex,&p1->job,&p1->num,p1->name);
if(p1->job=='s') scanf("%d",&p1->category.classes);
else if(p1->job=='t') s