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.
- [#1] – Introduction to C++ – Questions
- [#2] – Preparing and Running C++ Program – Questions
- [#3] – C++ Fundamentals – Questions
- [#4] – Data Input and Output – Questions
- [#5] – Control Structures – Questions
- [#6] – Functions – Questions
- [#7] – Arrays and Strings – Questions
- [#8] – Structures and Unions – Questions
- [#9] – Pointers – Questions
- [#10] – Object and Classes – Questions
- [#11] – Constructors and Destructors – Questions
- [#12] – Operator Overloading and Type Conversion – Questions
- [#13] – Inheritance – Questions
- [#14] – Virtual Functions – Questions
- [#15] – C++ Streams and Files – Questions
- [#16] – Templates – Questions
- [#17] – Exception Handling – Questions
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 –
- 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.
- 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.
- The structure definition creates structure variables and allocates storage space for them.
- The structure members can be accessed using a dot operator or an arrow operator.
- 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.
- Array of structures refers to an array in which each array element is a structure variable of same type.
- A structure variable can be passed either by value or by reference.
- A complete structure or a member of a structure can be passed to a function and can be returned from a function.
- 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 –
- 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.
- 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 ?
- How much memory is allocated when a structure variable is defined ?
- Equal to the size of the member of the structure.
- Can we assign one structure variable to another of same type ?
- Yes, as member by member copy is being made.
- 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 –
- What is a structure ? How is it declared ?
- Why structure are called heterogeneous data types ?
- How is memory allocated to the structure variables ?
- How are the members of structure accessed ?
- Can a structure be initialized ? If yes. How ?
- How can structure be nested ?
- How are the members of the nested structure accessed ?
- What is an array of structures ? How is it declared ?
- How is structure passed to a function ?
- How is structure returned from a function ?
- What is union ? How is it different from structure ?
- Discuss the different ways to initialize array of structures ?