Lecture 18: struct


struct

In the lecture 13 we learned how an array can store variables of the same type in a nicely ordered, indexed way, like in a file cabinet with in each drawer the same type of information. If we want to group variables together that are not of the same type, we can do this in a struct.
 

The three file cabinets store things of the
same type, just like arrays. The left one
could be "bytes", the middle one
"integers" and the right one "reals".

 

The file cabinet in the center is used for
storing things of mixed type. In the same
way, a struct is used for storing variables
of different type, integers, real, or
whatever, all together in the same box.


 
 A struct is a set of variables of mixed type. 


Declaring a struct


A visualization of a struct, namely a motley set of variables. Each variable inside a struct is called a field. Here we have 5 fields: an int (b), a float (f), a char (c), a single array of floats (r) and a double array of doubles (m).

To declare a struct we do the following
 

struct {
   type1 item1
   type2 item2;
      |
   typeN itemN;
} name;
with
name: the name for the variable holding the struct.
item1..itemN: the name for the various fields in the struct. These have the same rules as the other identifiers for variables, functions, etc. Note that we can put as many fields in the struct as we want, with any combination of types.
type1..typeN: the type of the fields of the struct. This can be any type of variable that we know, including structs!

As an example, the definition of a struct containing the information of a student might have fields for name, year, and tuition fees paid:
 struct {
   char name[20];
   int year;
   int propinas; 
 } student;
This struct can only contain the information of a single student. Later we will see how to make an array of structs.


Using a struct

To access a struct we use the format
 
 name.field 

For example, to assign values to the struct student we can do the following

  strcpy(student.name, "Peter Stallinga");
  student.year = 2002;
  student.propinas = 1;

for the explanation of the function strcpy, see the lecture on strings.
Another example:

  struct {
      float x;
      float y;
  } coordinate;

  coordinate.x = 1.0;
  coordinate.y = 0.0;

This is not exactly a set of variables of mixed type, so in principle we could also do this with an array:

  float coordinate[2];

  coordinate[0] = 1.0;
  coordinate[1] = 0.0;

but, the first version, with the struct is more logical.
Another example:

  struct {
    char rua[20];
    int numero;
    int andar;
    char porta;
  } address;

  strcpy(address.rua, "Rua Santo Antonio");
  address.numero = 34;
  address.andar = 3;
  address.porta = 'E';

  printf("%s %d", address.rua, address.numero);
  printf("%d %c", address.andar, address.porta);

Rua Santo Antonio 34
3 E


Arrays of structs, structs of arrays

The above examples use simple structs. With the information of the lecture on arrays (lecture 13), however, we know how to build an array that can store the information of many equal objects of any type, even structs. Let's build an array of 2000 students:
(Question: How many bytes does this variable occupy in memory? Answer at the end)
 
 struct {
   char name[20];
   int year;
   int propinas; 
 } students[2000];
  i = 1055;
  strcpy(students[i].name, "Peter Stallinga");
  students[i].year = 2002;
  students[i].propinas = 1;

Note the structure of arrays of structs. students is an array of structs, therefore, students[i] is one of these structs and if we want to assign something to a field we use the period and the fieldname, so students[i].year is an int containing the year of student number i.
Wrong syntax would be students.year[i] (we could use this if we had a single struct students containing a field year that is an array)
Also wrong: students.[i]year, which doesn't make sense at all. Be careful where you put the dots.

Later we will see that we can much more easily declare variables of type array of structs (see lecture 19).


Quick Test

To test your knowledge of what you have learned in this lesson, click here for an on-line test.
Answer to the question in the text: 2000 x (20+2+2) = 48000 bytes.

Peter Stallinga. Universidade do Algarve, 28 November 2002