CIS3355:
Business Data Structures |
The Structure Data Type
Structured data objects and ArraysWe have seen in previous sections that objects of simple data type can only store data of one type. That is why they are called simple objects. Whilst an array can provide storage for many data elements, each of these elements must be of the same type within any one array.There are many situations where we would like to store data of different types within the same data structure. If we were developing a data base program to store details of club membership, for instance, we would need to keep records of lots of individual members each of which has a name, address, age and telephone number. It would be convenient if we could refer to each members record as a single data object. On their own, simple data objects and arrays will not let us do this.
The declarations in the above example will only give us sufficient data objects
to store information for one club member. To store information about 100 members
we would need 100 copies of the above data objects, all with different
name tags.
We cannot incorporate the data objects into a 100 element array because there
are objects of different data type involved.
By combining simple data types and encapsulating them within a data type called a structure this limitation can be overcome. The StructureA structure is a mechanism which allows data objects of different types to be grouped together as a single compound type. The structure is a way of allowing programmers to declare and define their own abstract data types. A structure data type can be used within a program in the same way that a simple data type can be used to instantiate data objects. In particular the elements of an array can each store a complete structure object.The following glossary of terms may be useful:
Declaring a StructureA simplified version of the syntax used to declare a structure is:
where:
The new
abstract data type
can be conceptualized as a black box containing its data members.
Once the above structure has been declared we can now use it to declare an array
of members like so:
Here MemberList is declared as an array with 100 elements, each of
which can store an object of type Member.
Dot NotationOnce the data elements have been encapsulated within a structure their individuality is hidden. We can no longer refer to the variableAge
without first stating which particular Member object we are
referring to. In the MemberList array, for instance, there are 100
elements each containing identical data structures each containing instances of
the variable Age . So we need a mechanism which will allow us to
access the data fields within the structure. This mechanism is referred to as
the dot notation for reasons that will become obvious.
Suppose that we declare a single object of type
aMember is now a direct reference to a single instance of an object
of type Member .
The data field
For instance, we can assign a value to Age by making the assignment
We can extract the value from Age by writing
Arrays of StructuresConsider the declaration:
Here we have declared MemberList to be an array with 100 elements
each of which can store a structure of type Member .
We can use the subscript notation to access any particular element like so:
but we need to be very clear about what it is we have actually accessed.
Each element in the
We have not yet accessed any individual data members within this structure.
To do that we need to use the dot notation. We could access the
Similarly we can gain access to the Firstname array like this:
but this will only give us access to the whole FirstName array, not
to individual characters within the array. If we want to access the 5th
character within the FirstName in the 34th element of
MemberList we would need to write:
|