C语言里的指针和数组的问题
#include<stdio.h>#include<string.h>
struct exc
{
int num;
char name[10];
char job[10];
union
{
int banji;
char position[10];
}a;
}per[6];
struct exc exe(struct exc x)
{
printf("请输入编号:\n");
scanf("%d",&x.num);
printf("请输入姓名:\n");
scanf("%s",x.name);
getchar();
printf("请输入职业:\n");
scanf("%s",x.job);
getchar();
if(strcmp(x.job,"t")==0)
{
printf("请输入职务:\n");
scanf("%s",x.a.position);
getchar();
}
else if(strcmp(x.job,"s")==0)
{
printf("请输入班级:\n");
scanf("%d",&x.a.banji);
getchar();
}
return x;
}
int main(void)
{
struct exc a[10];
int p,i;
for(i=0,p=0;i<6;i++,p++)
{
a[p]=exe(per[i]);
}
printf("编号\t姓名\t身份\t职位or班级\n");
for(p=0;p<6;p++)
{
if(strcmp(a[p].job,"s")==0)
{
printf("%4d\t%s\t%s\t%d\n",a[p].num,a[p].name,a[p].job,a[p].a.banji);
}
else if(strcmp(a[p].job,"t")==0)
{
printf("%4d\t%s\t%s\t%s\n",a[p].num,a[p].name,a[p].job,a[p].a.position);
}
}
}这样行
#include<stdio.h>
#include<string.h>
struct exc
{
int num;
char *name;
char *job;
union
{
int banji;
char position[10];
}a;
}per[6];
struct exc exe(struct exc x)
{
printf("请输入编号:\n");
scanf("%d",&x.num);
printf("请输入姓名:\n");
scanf("%s",x.name);
getchar();
printf("请输入职业:\n");
scanf("%s",x.job);
getchar();
if(strcmp(x.job,"t")==0)
{
printf("请输入职务:\n");
scanf("%s",x.a.position);
getchar();
}
else if(strcmp(x.job,"s")==0)
{
printf("请输入班级:\n");
scanf("%d",&x.a.banji);
getchar();
}
return x;
}
int main(void)
{
struct exc a[10];
int p,i;
for(i=0,p=0;i<6;i++,p++)
{
a[p]=exe(per[i]);
}
printf("编号\t姓名\t身份\t职位or班级\n");
for(p=0;p<6;p++)
{
if(strcmp(a[p].job,"s")==0)
{
printf("%4d\t%s\t%s\t%d\n",a[p].num,a[p].name,a[p].job,a[p].a.banji);
}
else if(strcmp(a[p].job,"t")==0)
{
printf("%4d\t%s\t%s\t%s\n",a[p].num,a[p].name,a[p].job,a[p].a.position);
}
}
}换成指针为什么就运行不了了。求解