Variables only store one piece of information, whereas arrays of specific data types hold information of the same data type. Variables and arrays may deal with a wide range of circumstances. However, we frequently have to deal with a collection of disparate data kinds. C++ provides a data type called’structure’ to deal with scenarios where it is necessary to hold incompatible data types. The structure is a C++ data type that can be defined by the user. Structures are used to aggregate multiple sorts of data, whereas arrays are used to combine the same type of data.
Another option for storing data of disparate data kinds would have been to create distinct arrays, but this would have to be disorganised. It is important to remember that structure elements are always stored in contiguous memory locations.
Features of Structs
- Using the assignment operator, we can assign the values of one structural variable to another of the same type.
- Structures can be nested within other structures, which means that structures can have members that are themselves structures.
- The structure variable can be passed to a function. As an argument, we can pass the individual structure elements or the complete structure variable to the function. A structural variable can also be returned by a function.
- A pointer can point to a struct in the same way that a pointer can point to an int or a pointer can point to a char variable.