共用体不是只能存一个数吗,这个怎么能存两个?
程序代码:
#include<stdio.h> struct { int num; char name[10]; char sex; char job; union { int clas; char position[10]; }category; }person[2]; int main() { int i; for (i = 0; i < 2; i++) { printf("Please enter the data of person:\n"); scanf_s("%d %s %c %c", &person[i].num, &person[i].name, 10, &person[i].sex, 1, &person[i].job, 1); if (person[i].job == 's') scanf_s("%d", &person[i].category.clas); else if (person[i].job == 't') scanf_s("%s", &person[i].category.position, 10); else printf("Input error!"); } printf("\n"); printf("No. name sex job class/position:\n"); for (i = 0; i < 2; i++) { if (person[i].job == 's') printf("%d %7s %6c %3c %7d\n", person[i].num, person[i].name, person[i].sex, person[i].job, person[i].category.clas); else printf("%d %7s %6c %3c %7s\n", person[i].num, person[i].name, person[i].sex, person[i].job, person[i].category.position); } return 0; }