2.7 Structures

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.

Introduction

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.

Declarations and Usage of Structures

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:

  • A unique student number, which could be represented as a string (an array of char).
  • The student’s name, which could be represented as a string (an array of char).
  • Final mark for the Introduction to computer science course, which is a floating point value (a float).

Creating Structures as New Data Types

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>;
     };
Definition ends with semicolon (;)

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.

Creating variable of a struct type

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:

struct Date
    {
      int day;
      int month;
      int year;
    };

We can now use this Date type, together with other types, as members of a Student type which we can declare as follows:

struct Student
    {
       char studentID[10];
       char name[30];
       float markCS ;
       Date  dateOfBirth;
    };

Or

struct Student 
{
    char studentID[10];
    char name[30];
    float markCS;
    struct Date {
      	          int day;
                  int month;
      	          int year;
                } dateOfBirth;
};


We can also declare structured variables when we define the structure itself:

struct  Student 
{
    char studentID[10];
    char name[30];
    float markCS ;
    Date  dateOfBirth;
} a, b, c;

C permits to declare untagged structures that enable us to declare structure variables without defining a name for their structures. For example, the following structure definition declares the variables a, b, c but omits the name of the structure:

struct 
{
    char studentID[10];
    char name[30];
    float markCS ;
    Date  dateOfBirth;
} a, b, c;

A structure type cannot contain itself as a member, as its definition is not complete until the closing brace (}). However, structure types can and often do contain pointers to their own type. Such self-referential structures are used in implementing linked lists and binary trees, for example. The following example defines a type for the members of a singly linked list:

struct List 
{   struct Student stu;     // This record's data.
    struct List *pNext;    // A pointer to the next student.
};

Referencing Structure Members with the Dot Operator

Whenever we need to refer to the members of a structure, we normally use the dot operator.

For example, if we wanted to access the number member of newStudent we could do so as follows:

newStudent.studentID

We can then access the member as if it were a normal variable. For instance, we can write to this member as follows.

newStudent.studentID= “C0681008”;

We can also read from the member in a similar fashion.

printf("Student identification: %s", newStudent.studentID);

The following code outputs the contents of an Student structure.

printf("Student Details\n");
printf("Identification: %s\n", newStudent.studentID);
printf("Name: %s\n", newStudent.name);
printf("Mark: %.2f\n", newStudent.markCS);
printf("Date of Birth: %i/%i/%i\n", 
             newStudent.dateOfBirth.day,
             newStudent.dateOfBirth.month,
             newStudent.dateOfBirth.year
);

Suppose we wish to input the details of this employee from a user. We could do so as follows.

Student newStudent;
printf("Enter student identification: ");
scanf("%s", &newStudent.studentID);
printf("Enter student name: ");
fflush(stdin);gets(newStudent.name);
printf("Enter mark for Introduction to computer science course: ”);
scanf("%f", &newStudent.markCS);
printf("Enter birth date (dd/mm/yyyy): ");
scanf("%i/%i/%i",
&newStudent.dateOfBirth.day,
&newStudent.dateOfBirth.month,
&newStudent.dateOfBirth.year
);

Initializing Structure Variables

When we declare a new variable of a basic data type we can initialize its value at declaration. We can also initialize structure variables at declaration as shown below.

Student newStudent = {
            "C0681008",
            "Cao Anh Huy",
            8.50,
            {1, 2, 1985}
        };

Notice how we include the initialization values in curly brackets, just like when we initialize an array. Furthermore, we include the values for any nested structure type (in this case the dateOfBirth member is a nested structure), in a further set of curly brackets.

Copying Structure Variables

One of the most convenient features of structures is that we can copy them in a single assignment operation. This is unlike an array, which must be copied item-by-item. The name of a structure variable when it appears on its own represents the entire structure. If a structure contains an array as a member, that array is copied if the entire structure is copied.

Student newStudent1, newStudent2;
// Get the values for newStudent2
...
// Copy newStudent2's value to newStudent1
newStudent1 = newStudent2;

Comparing Values of Structures

We cannot compare structures in a single operation. If we wish to compare the values of two structure variables, we need to compare each of their members.

Arrays of Structures

Just as we can have an array of basic data types, we can also have an array of structures. Suppose that we created an array of Student structures as follows. We could then copy newStudent into each position in the array.

Student  students[100];
Student  newStudent;
int i;
for (i=0; i<100; i++)
{
// Get the values for newStudent
...
// Copy into the next position in the array
students[i] = newStudent;
}

Operations on Structures

Passing Structures to and from Functions

Structures can be passed to functions just like any other data type. Functions can also return structures, just as they can return any basic type. Structures can be also be passed to functions by reference.

Just like passing variable of a basic data type, when we pass a structure as an argument to a function, a copy is made of the entire structure. Structures are passed by value. We can easily take the code that output a student and put it into a function as follows.

void outputStudent(Student stu) 
{
printf("Student Details\n");
printf("Identification: %s\n", stu.studentID);
printf("Name: %s\n", stu.name);
printf("Mark: %.2f\n, stu.markCS);
printf("Date of Birth: %i/%i/%i\n",
           stu.dateOfBirth.day,
           stu.dateOfBirth.month,
           stu.dateOfBirth.year 
      );
}

If we had an array of 100 students and wanted to output them this would be straightforward:

Student students[100];
int i;
...
for (i=0; i<100; i++) {
   outputStudent(students[i]);
}

We could similarly place the code to input a student into a function, but now we have a problem. The function can return a structure of type Student as follows.

Student inputStudent() 
{
   Student tempStudent;
   printf("Enter Student identification: ");
   scanf("%s", &tempStudent.studentID);
   printf("Enter Student name: ");
   fflush(stdin);gets(tempStudent.name);
   printf("Enter final mark: ");
   scanf("%f", &tempStudent.markCS);
   printf("Enter birth date (dd/mm/yyyy):");
   scanf("%i/%i/%i",
         &tempStudent.dateOfBirth.day,
         &tempStudent.dateOfBirth.month,
         &tempStudent.dateOfBirth.year
        );
   return tempStudent;
}

In the example above we are filling the structure variable tempStudent with values. At the end of the function, the value of tempStudent is returned as the return value of the function. The code to input 100 students can now be modified to use this function:

Student students[100];
int i;
for (i=0; i<100; i++) {
    students[i] = inputStudent();
}

The Arrow Operator

In order to dereference a pointer we would normally use the dereferencing operator (*) and if we our pointer was to a structure, we could subsequently use the dot ‘.’ operator to refer to a member of the structure. Suppose we have declared a pointer which could be used to point to a structure of type employee as follows.

Student stuVariable;
Student *stuPtr;
stuPtr = &stuVariable;

To refer to the student identification we could say:

(*stuPtr).studentID

Note that the brackets are necessary because the dereference operator has lower precedence than the dot operator. This form of syntax is a little cumbersome, so another operator is provided to us as a convenient shorthand:

stuPtr->studentID

This method of accessing the number member through a pointer is completely equivalent to the previous form. The ‘->’ operator is called the indirect member selection operator or just the arrow operator and it is almost always used in preference to the previous form.

Passing Structures by Reference

Passing structures to a function using pass-by-value can be simple and successful for simple structures so long as we do not wish to do so repeatedly. But when structures can contain a large amount of data (therefore occupying a large chunk of memory) then creating a new copy to pass to a function can create a burden on the memory of the computer. If we were going to do this repeatedly (say several thousand times within the running of a computer) then there would also be a cost in time to copy the structure for each function call.

In the example at the beginning of this section we created and filled a structure variable called tempStudent. When the function ended it returned the value of tempStudent. The same inefficiency exists with the return value from the function, where the Student structure must be copied to a local variable at the function call.

Whether such inefficiencies are of any significance or not depends on the circumstances and on the size of the structure. Each Student structure probably occupies about 50 bytes, so this is a reasonably significant amount of memory to be copying each time the output function is called or each time the input function returns, especially if this is happening frequently.

A better solution would be to pass the Student structure by reference, which means we will pass a pointer to the structure.

We can now revise the input function by passing an Student structure by reference using a pointer. Because the function is no longer returning an Student structure, we can also enhance the function to return a Boolean status indicating whether an Student structure was successfully read or not. We can enhance our function to do some better error checking. Below is the revised version.

bool inputStudent(Student *stuPtr)
{
printf("Enter Student identification: ");
if (scanf("%s", &stuPtr->studentID) != 1) return false;
printf("Enter Student name: ");
fflush(stdin);gets(stuPtr->name);
printf("Enter mark: ");
if (scanf("%f", &stuPtr->markCS) != 1) return false;
printf("Enter birth date: ");
if (scanf("%i/%i/%i",&stuPtr->dateOfBirth.day,
&stuPtr->dateOfBirth.month,&stuPtr->dateOfBirth.year) != 3) 
   return false;
return true;
}

The code to input 100 students can now be revised as follows.

Student students[100];
int i;
for (i=0; i<100; i++) 
{
   while (!inputStudent(&students[i])) 
   {
printf("Invalid student details - try again!\n");
fflush(stdin);
   }
}

As a final example, consider a function to give s student a mark rise. The function takes two parameters. The first is an Student structure passed by reference, (a pointer to an Student structure) and the second is the increase of mark.

void markRise(Student *stuPtr, float increase)
{
stuPtr->markCS += increase;
}

What use is such a function? Having input many students into an array, we might then wish to give certain students a mark rise. For each student we can easily call this function, passing a pointer to the appropriate Student structure.

Enumerated Types

Another way of creating a new type is by creating an enumerated type. With an enumerated type we build a new type from scratch by stating which values are in the type. The syntax for an enumerated type is as follows.

enum TypeIdentifier { list... };

Here is an example of a definition of an enumerated type that can be used to refer to the days of the week.

enum DayOfWeek {sun, mon, tue, wed, thu, fri, sat};

Just like when we define a structure type, defining an enumerated type does not give us any space to store information. We use the type like a template to create variables of that type.

For instance we can create a variable of type DayOfWeek as follows.

DayOfWeek nameOfDay;

With variables of enumerated types we can do almost anything we could do with a variable of a basic data type. For instance we can assign a value as follows.

nameOfDay = tue;

Note that tue is a literal value of type DayOfWeek and we do not need to place quotes around it.

The values in DayOfWeek are ordered and each has an equivalent int value; sun==0, mon==1, and so on. The value of sun is less than the value of wed because of the order they were presented in the list of values when defining the type. We can compare two values of enumerated types as follows:

DayOfWeek day1, day2;
// Get day values
...
if(day1 < day2) {
...
}

Here is another example that uses enumerated types.

#include <stdio.h>
#include <conio.h>
enum TrafficLight {red, orange, green};
int main() 
 {
   TrafficLight light;
   printf("Please enter a Light Value: (0)Red (1)Orange (2)Green:\n");
   scanf("%i", &light);
   switch(light)
    {
      case red:
         printf("Stop!\n");
         break;
      case orange:
         printf("Slow Down\n");
         break;
      case green:
         printf("Go\n");
    }
   getch();
How to Reuse & Attribute This Content
© Jul 29, 2009 Huong NguyenTextbook content produced by Huong Nguyen is licensed under a Creative Commons Attribution License 3.0 license.
Under this license, any user of this textbook or the textbook contents herein must provide proper attribution as follows:
The OpenStax name, OpenStax logo, OpenStax book covers, OpenStax CNX name, and OpenStax CNX logo are not subject to the creative commons license and may not be reproduced without the prior and express written consent of Rice University. For questions regarding this license, please contact support@openstax.org.
  }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s