Programming Intermediate – Module 6
Structures – Why do we need them?
imagine having to store info about students attending a course. info is needed like name, time
spent studying and number of the last completed chapter.
this can be done with the declaration of char student_name[100000][26]//to store number of
student and then name respectively
trying to store the name of the first student could store info in the array like
strcpy(student_name[0], “Bond”);
the time spend on the time spend on the site will be stored as a float like so:
float student_time[100000];
knowing Bond spend three and a half hours on the course can denote in the following way:
student_time[0] = 3.5;
the primary objection is that the data regarding the same object (a student) is dispersed between
three variables, although logically it should be in one consolidated unit. handling
multiple arrays is cumbersome and error-prone.
an array is an aggregate of elements. the elements are numbered and are of the same type. can we
use an aggregate whose elements could be of different types? Could they be identified by
names, not by numbers? is it even a good idea? YES
the aggregate we’re thinking of is called a structure
A structure contains any number of elements of any type. Each of these elements is called a
field. Each field is identified by its name, not by its number. Obviously, the field names
must be unique and cannot be doubled within a single structure. syntactically a structure
looks like so:
struct STUDENT {
char name[26];
float time;
int recent_chapter;
};
the declaration of the structure always starts with the keyword [struct];
a struct tag is placed after the keyword (STUDENT) in this case); it’s the name of the
structure itself; there’s a widely accepted custom of composing structure tags with capital
letters only to distinguish them form ordinary variables
here comes the opening curly bracket – a sign that the field declaration begins at this
point;
our structure has three fields: the first is a 26-element array of type char and is called
name; the second is a float and is called time, and the third is an int and is called
recent_chapter
the declaration ends with the closing curly bracket followed by a semicolon
we should emphasize that the previous declaration doesn’t create a variable, but only
describes the structure we’re going to use in our program. if we want to declare a
variable as a structure, we do it as follows:
struct STUDENT stdnt;
this declaration sets up a variable (a structured variable) named stdnt. The variable stdnt is of
type struct STUDENT. We know that this variable consists of three named fields, but we
don’t know how to access them.
As the “C” language offers a specialized indexing operator [] for arrays, we also have the
selection operator, designed for structures and denoted as a single character “.” (dot).
the priority of the selection operator is very high, equal to the priority of the [] operator.
This is a inary operator. Its left argument must identify the structure while the right argument
must be the name of the field known in this structure.
the result of this operator is the selected field of structure, and therefore the expression
containing this operator is sometimes called a selector. this means that the selector here:
stdnt.time
results in the selection of a field named time. the ypte of this expression is the type of the
selected field and this expression is an l-value(L-value).
Consequently, you can use both of these selectors:
stdnt.time = 1.5;
and
float t;
t = stdnt.time;
a structure can not be a field of itself
structures can be aggregated inside an array so if we wantto declare an array of STUDENT
structures, we can do it in this way:
struct STUDENT stdnt[100000];
Access to the selected fields requires two subsequent operations:
in the first step, the [] operator indexes the array in order to access the structure
we need
in the second step, the selection operator selects the desired field
This means that if we want to select the time field of the fourth stdnts' element, we write it as follows:
stdnts[3].time
We’ve collected all these assignments which have been performed for the three
separate arrays. Analyze them carefully:
strcpy(stdnts[0].name, "Bond");
stdnts[0].time = 3.5;
stdnts[0].recent_chapter = 4
Two structures can contain fields with the same names – the snippet in the editor is correct.