Please provide brief answers to the following questions What
Please provide brief answers to the following questions
What is a structure type?
What is the difference between a structure type and an array?
How do you access a member of an object of a structure type?
Solution
A structural type system (or property-based type system) is a major class of typesystem, in which type compatibility and equivalence are determined by the type\'sactual structure or definition, and not by other characteristics such as its name or place of declaration.
Both the arrays and structures are classified as structured data types as they provide a mechanism that enable us to access and manipulate data in a relatively easy manner. But they differ in a number of ways listed in table below:
Arrays
Structures
3. But in the case of structure, first we have to design and declare a data structure before the variable of that type are declared and used.
A data structure is a group of data elements grouped together under one name. These data elements, known asmembers, can have different types and different lengths. Data structures can be declared in C++ using the following syntax:
struct type_name {
member_type1 member_name1;
member_type2 member_name2;
member_type3 member_name3;
.
.
} object_names;
Where type_name is a name for the structure type, object_name can be a set of valid identifiers for objects that have the type of this structure. Within braces {}, there is a list with the data members, each one is specified with a type and a valid identifier as its name.
For example:
This declares a structure type, called product, and defines it having two members: weight and price, each of a different fundamental type. This declaration creates a new type (product), which is then used to declare three objects (variables) of this type: apple, banana, and melon. Note how once product is declared, it is used just like any other type.
Right at the end of the struct definition, and before the ending semicolon (;), the optional field object_names can be used to directly declare objects of the structure type. For example, the structure objects apple, banana, and melon can be declared at the moment the data structure type is defined:
In this case, where object_names are specified, the type name (product) becomes optional: struct requires either atype_name or at least one name in object_names, but not necessarily both.
It is important to clearly differentiate between what is the structure type name (product), and what is an object of this type (apple, banana, and melon). Many objects (such as apple, banana, and melon) can be declared from a single structure type (product).
| Arrays | Structures |
| 1. An array is a collection of related data elements of same type. | 1. Structure can have elements of different types |
| 2. An array is a derived data type | 2. A structure is a programmer-defined data type |
| 3. Any array behaves like a built-in data types. All we have to do is to declare an array variable and use it. | 3. But in the case of structure, first we have to design and declare a data structure before the variable of that type are declared and used. |

