Structures and Unions


1. What is a structure?
Ans:
i) It is a collection of variables of multiple types
OR
ii) It is a collection of heterogeneous elements

2. Benefits of structure?
Ans:
It allows related data elements (fields) of an entity to be combined & manipulated as a single unit.

Ex1: writing entire structure object at once into a file.
Ex2: parsing structures to functions
Ex3: assigning structure objects
Ex4: assigning arrays (if put inside a structure)

3. What is an union?
Ans:
It is similar to a structure – except that the memory allocated for a union variable is : memory required for the largest field. At a time only one field’s value can be stored.

4. Difference between structures and unions?
Ans:
The memory allocated for structure is equal to the total number of bytes allocated to all the members of structure.
structures can be declared locally or globally.
The memory allocated for union is equal to the size of the highest datatype member.
unions can only be declared locally.

5 .Can a structure contain a pointer to itself?
Ans:Yes.These type of structures are called self referential structures.

6. How the structure members are accessible?
Ans:If the variable is normal variable . operator is used.
syntax: structure variable.structure member

If the variable is a pointer variable ---> operator is used.
syntax: structure variable--->structure member