1. 程式人生 > >共用體Union的例子

共用體Union的例子

共用記憶體,只能看到最後的分量。

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

union student
{
            int id;
            char name[20];
            float percentage;
};

int main()
{
            union student record;
            record.id=1;

            strcpy(record.name, "Raju");
            record.percentage = 86.5;
            // printf(" Id is: %d \n", record.id);
            printf(" Name is: %s \n", record.name);
            printf(" Percentage is: %f \n", record.percentage);
            return 0;
}