<< Chapter < Page | Chapter >> Page > |
A structure type can contain a number of dissimilar data objects within it. Unlike a simple variable (which contains only one data object) or an array (which, although it contains more than one data item, only contains items of a single data type),a structure is a collection of related data of different types. a name, for example, might be array of characters, an age might be integer. A structure representing a person, say, could contain both a name and an age, each represented in the appropriate format.
Until now, all the data that we have dealt with has been either of a basic type such as char, int and double…, or an array of those types. However, there are many situations in real life where a data item needs to be made up from other more basic types. We could do this with an array if the constituent types were all the same, but often they are different. For example, suppose we want to record the details of each student in a class. The detail of each student might be as follow:
The definition of a structure type begins with the keyword struct, and contains a list of declarations of the structure's members, in braces:
struct structTag
{<list of members>;
};
The three components above can be placed in a structure declared like this:
struct Student
{char StudentID[10];char name[30];float markCS ;
};
The keyword struct introduces a structure declaration, which is a list of declarations enclosed in braces. An optional name called a structure tag may follow the word struct (as with Student here). The tag names this kind of structure, and can be used subsequently as a shorthand for the part of the declaration in braces. The variables named in a structure are called members. A structure member or tag and an ordinary (i.e., non-member) variable can have the same name without conflict, since they can always be distinguished by context. Furthermore, the same member names may occur in different structures, although as a matter of style one would normally use the same names only for closely related objects.
Structure types are not considered a variable declaration, just definition of a new type, so they cannot store anything until we declare variable of this type. Here is how we would create:
type_name_of_struct name_of_variable;
Creating three variables a, b, c of the Student type:
Student a, b, c;
Creating an array of the Student type:
Student studentCS[50];
A member of a structure may have any desired complete type, including previously defined structure types. They must not be variable-length arrays, or pointers to such arrays. For instance, now we want to record more information of students, for example their date of birth, which comprises the day, month and year. So first, let's start with the date, because that is a new type that we may be able to use in a variety of situations. We can declare a new type for a Date thus:
Notification Switch
Would you like to follow the 'Introduction to computer science' conversation and receive update notifications?