Data Structures

There are often occasions when it is desirable to view a group of data items with different data types as a single entity. A student record in a database, for example, might consist of the student's name, address, telephone number, date of birth and student ID number. These data items provide information about the student, and each student record will consist of a similar set of data items. A structure is a set of related data elements grouped together as a single entity. Each data element is a member of the structure, with its own data type. Structures are declared in C++ using the following syntax:

typedef struct
{
  type variable_name_1;
  type variable_name_2;
  type variable_name_3;
  .
  .
  .
} struct_var;

In the above example, a structure variable called struct_var is created. The data members are listed between the opening and closing curly braces ({ }). The declaration for each data member specifies the member's data type, followed by its variable name (or identifier). Once the structure has been declared, objects of this new type can be declared and initialised in the program in the same way as other types of variable. The following code fragment creates a structure type called stock_item and declares two objects of that type:

struct
{
  int quantity;
  float value;
} stock_item;
.
.
.
stock_item widget, grommet;

An object such as widget is said to be an instantiation of a structure type (in this case stock_item). Once a structure type has been declared, we can create as many objects of that data type as we like. In order to work with the individual structure elements for an object of a given structure type, we need to identify both the object and the element within that object with which we wish to work. The following code fragment assigns values to the data items in the object widget:

widget.quantity = 24;
widget.value = 2.49;

Note that each element is referenced using the object's name, and the name of the data element, separated by a period. The simple program below demonstrates the creation and use of a structure.

// Example program 1
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

typedef struct
{
  char title[50];
  int year;
}film;

film favourite;

void show_favourite(film fav_film);

void main()
{
  printf("Enter the title of your favourite film: ");
  gets(favourite.title);
  printf("\nEnter the year the film was released: ");
  scanf("%d", &favourite.year);
  printf("\nYour favourite film is: ");
  show_favourite(favourite);
  printf("\n\nPress any key to exit . . . ");
  getch();
}

void show_favourite (film fav_film)
{
  printf("%s (%d)\n", fav_film.title, fav_film.year);
}

The output from example program 1 is shown below.


The output from example program 1

The output from example program 1


Note that a structure variable can be passed to a function as a parameter in the same way that standard variable types can. We can also create an array of structures, as demonstrated by the following program:

// Example program 2
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

typedef struct
{
  char title[50];
  int year;
} film;

film my_films[3];

void show_film(film list_film);

void main()
{
  int i, year;

  for (i=0; i<3; i++)
  {
    printf("\nEnter the title of film number %d: ", i+1);
    gets(my_films[i].title);
    printf("Enter the year the film was released: ");
    scanf("%d", &my_films[i].year);
    fflush(stdin);
    }

  printf("\n\nYou have listed the following films:\n\n");
  for (i=0; i<3; i++) show_film(my_films[i]);
  printf("\n\nPress any key to exit . . . ");
  getch();
}

void show_film (film list_film)
{
  printf("%s", list_film.title);
  printf(" (%d)\n", list_film.year);
}

The output from example program 2 is shown below.


The output from example program 2

The output from example program 2



Pointers to structures

As with other variable types, it is possible to create pointers to structure variables. The following code fragment demonstrates this:

struct
{
  string title;
  int year;
} film;

film myfilm;
film * ptr_myfilm;

Here, myfilm is an object of type film, and ptr_myfilm is a pointer to an object of type film. The following statement sets ptr_myfilm to point to myfilm:

ptr_myfilm = &myfilm;

The following short program demonstrates how pointers can be used with structures:

// Example program 3
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

typedef struct
{
  char title[50];
  int year;
} film;

void main ()
{
  film myfilm;
  film * ptr_myfilm;
  ptr_myfilm = &myfilm;

  printf("Enter the title of your favourite film: ");
  gets(myfilm.title);
  printf("\nEnter the year the film was released: ");
  scanf("%d", &myfilm.year);
  printf("\nYour favourite film is: ");
  printf("%s (%d)", ptr_myfilm->title, ptr_myfilm->year);
  printf("\n\nPress any key to exit . . . ");
  getch();
}

Note the use here of the arrow operator (->), which is used exclusively to dereference struct members. The output from example program 3 is shown below.


The output from example program 3

The output from example program 3



Nested structures

We have said already that structure members can be of any valid data type, and this includes other structures, as demonstrated in the following code fragment:

typedef struct
{
  char title[50];
  int year;
} film;
.
.
.
type struct
{
  char name[20];
  char email[100];
  film fav_film;
} film_club_member;
.
.
.
film_club_member sara, alan, simon, olga;