Home » Programming & Data Structure » Programming and data structure miscellaneous » Question

Programming and data structure miscellaneous

Programming & Data Structure

  1. Consider the following C declaration
    struct {
        short s [5]
       union {
       float y;
       long z;
       } u;
    } t;
    Assume that objects of the type short, float and long occupy 2 byte, 4 byte and 8 byte, respectively. The memory requirement for variable t, ignoring alignment considerations, is
    1. 22 byte
    2. 14 byte
    3. 18 byte
    4. 10 byte
Correct Option: C

The amount of memory required to store a structure variable is the sum of the sizes of all its members. But in the case of union, the amount of memory required is the amount required by its largest member. Therefore u, which is a union member of the struct, occupies only 8 bytes of memory, because the largest memory is 8 bytes consumed by long z;. Another member in the struct is short s [5], this will occupy 10 bytes of memory (2 bytes x 5).



Your comments will be displayed only after manual approval.