21)
Program 20 - Structures
Program 20 | Structures | Source code - prog20.c |
Structures effectively allow you to write
your own custom data types. They allow a program to store
data which maps better to real life data. The program below, shows how to define a structure, store information into it, and then retrieve information back. |
Program code- new parts shown in red |
|
Description of the program code |
The first new chunk
of code, in red, defines a structure template
called PERSON, which groups together a selected amount of
attributes of a person ( name, age and sex ). Even though
C has only a few simple data types complex structures can
be created by combining them. A persons name can be stored in an array of characters, in the example code, a person can't have a name consisting of more than 100 characters. A persons age can be stored in an unsigned char ( 0 - 255 range ). Finally a persons sex ( male or female ) can be stored as a single character ( m or f ). The structure template defines all of this. The template is given a name which can consist of the same characters used with variable and function naming. The only reason it is in upper case is because of the style I prefer to use, with structure templates always being made up from capital letters, it doesn't have to be the case though. After there is a template defined, the template can be used to create a variable from it. struct PERSON user; This is the same as declaring a variable called user, but instead of using one of the basic C data types, the PERSON structure template is used ( it has to have struct in front of it though ). Now a variable has been defined called user to hold a persons details, it can be used. The rest of the program shows how by using the dot notation. So for example, to store the age 73, you would used a line user.age=73; The program above first, gets the information from user input, and then displays the information. There is another way to use structures, one which I personally prefer, because it doesn't require the struct keyword to be used when defining a variable for the template.
Both methods work in the same way, but with this latter method you can declare variables PERSON user; in the same way as declaring other variables. The typedef keyword can be placed at the start of the structure definiton, to effectively create a data type. Also the useable data type name has to be placed at the end of the structure template, it doesn't have to be the same as the top name though. You could for example have :
The part that is used as the data type name reference is the name given at the end of the structure template though ( PERSON ). There is a reason for two names having to be given, which will be covered when a page on dynamic memory allocation is done. The examples above are okay if you just want to store one person, but it is easy to extend this by using an array to store more. The example below is the same as the previous examples, but this time can store 100 people. Only the first persons details are entered and displayed though in the code ( index 0 )
You can even have structures in structures to create even more complex data types :
The person structure above is extended to include basic salary details as well. Using the example where an array of 100 people were created called user, the following line could be used to set the annualearnings to £20,000 for the 10th person in the array ( index 9 ). user[9].sal.annualearnings=20000; All structures that are referenced have to be defined higher in the code than where they are used, which is why the SALARY structure is defined first, because the PERSON structure references it. |
Summary |
This page showed
how data can be stored in structures, which are made up
of basic C data types to form memory structures that
resemble more closely to real life objects. Out of the
handful of basic data types there are, it is possible to
"model" any real life structure in a program. Structures when used with arrays let a program easily manipulate large amounts of data held in a single variable, which is where they are the most useful. Combined with a knowledge of file handling ( next ) and finally pointers structures become a crucial element for most programs. |
Tasks |
20.1) Extend the program above so that it gets information on 5 people from the user, clears the screen and displays information on all 5. |
(c) J.C.Spooner 2001