101 结构体的初始化与存取

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#if 0
struct tagIDCard {
    long serialNo;    // 6位数的序列号
    char name[32];    // 名称
};

struct tagStudent {
    char gender;    // F or M
    unsigned short age;
    struct tagIDCard id;
};
#else
typedef struct tagIDCard {
    long serialNo;    // 6位数的序列号
    char name[32];    // 名称
} IDCard;

typedef struct tagStudent {
    char gender;    // F or M
    unsigned short age;
    IDCard id; 
} Student;
#endif

void print_student(Student *ps)
{
    if (ps) {
        if (ps->id.serialNo < 200000)
            ps->id.serialNo += 200000;
        printf("%15s: %c\n"
            "%15s: %hd\n"
            "%15s: %ld\n"
            "%15s: %s\n",
            "Gender", ps->gender,
            "Age", ps->age,
            "ID SerialNo.", ps->id.serialNo,
            "ID name", ps->id.name);
    }   
}

int main(int argc, char *argv[])
{
    IDCard id101 = { 200101, "x1" };
    Student student = {
        'M',
        21,
        { 0, "" }
    };
    student.id.serialNo = 200100;
    strcpy(student.id.name, "x");
    print_student(&student);
    student.id = id101;
    print_student(&student);
    return 0;
}