[#8] – Structures and Unions – Questions

C-plus-plus-programming-questions-techhyme

A structure is a collection of related data items which can be of different types held together in a single unit. The data items enclosed within a structure are known as its members which can be either of int, float, char etc types. As a structure may contain items of different types so it can be viewed as a collection of heterogeneous data items.

Index:

The variables of structure type can be created through which we can manipulate its members. This property help us to organize complex data items efficiently in a program.

Key Points To Remember –

  1. A structure is a collection of related data items which can be of different types. These data items are held together into a single unit. The data items enclosed within a structure are known as its members. As structure may contains items of different types so it can be viewed as a collection of heterogeneous data items.
  2. The structure declaration starts with a structure header that consists of keyword struct followed by the tag that serves as a structure name which can be used for creating structure variables.
  3. The structure definition creates structure variables and allocates storage space for them.
  4. The structure members can be accessed using a dot operator or an arrow operator.
  5. In a nested structure, a structure is embedded within another structure. This embedding can be done to any depth. The structure to be embedded must be declared before it can be used.
  6. Array of structures refers to an array in which each array element is a structure variable of same type.
  7. A structure variable can be passed either by value or by reference.
  8. A complete structure or a member of a structure can be passed to a function and can be returned from a function.
  9. Union is different from a structure. The memory allocated to a union is equivalent to the memory allocated to member with maximum size where as in structure, the memory allocated is equivalent to the sum of memory allocated to all its members.

Viva Voce Questions – 

  1. Can we initialize the members of a structure during its declaration ?
    • No, it is just a blueprint on the basis of which structure variables are created.
  2. How does structure in C differ from that of structure in C++ ?
    • Structure in C only contains data whereas structure in C++ contains data as well as associated functions ?
  3. How much memory is allocated when a structure variable is defined ?
    • Equal to the size of the member of the structure.
  4. Can we assign one structure variable to another of same type ?
    • Yes, as member by member copy is being made.
  5. What is the difference between a structure and union ?
    • The size of the structure is equal to size of the sum of its members whereas the size of union is equal to the size of the member with maximum size.

Other Similar Questions – 

  1. What is a structure ? How is it declared ?
  2. Why structure are called heterogeneous data types ?
  3. How is memory allocated to the structure variables ?
  4. How are the members of structure accessed ?
  5. Can a structure be initialized ? If yes. How ?
  6. How can structure be nested ?
  7. How are the members of the nested structure accessed ?
  8. What is an array of structures ? How is it declared ?
  9. How is structure passed to a function ?
  10. How is structure returned from a function ?
  11. What is union ? How is it different from structure ?
  12. Discuss the different ways to initialize array of structures ?
You may also like:

Related Posts

Leave a Reply