INFORMATION & COMPUTER SCIENCE DEPARTMENT, KFUPM

ICS – 201 SECTION 55 & 56  (992 Semester)

INTRODUCTION TO COMPUTER SCIENCE

LAB  #07 TOPIC: Structures & Functions

Instructor: Bashir M. Ghandi

 

Objective:       Understand how to declare and use structures

 


Declaration of Structures

·        The struct (structure) specifier is used to declare a data structure that can store heterogeneous data elements.

·        The elements can be of any type including enumerated types, arrays and even other structures.

E.g:  struct user_record {int id_no;

                          char name[40];

                          char dept[40];

                          int no_of_books;

                          USER_STATUS status;

                         };

Note:  USER_STATUS is enum type with values: STUDENT, FACULTY

·        A variable with the above structure can be declared as:

struct user_record user;

·        We can simplify the variable declaration above, if we use typedef in the declaration.  The above can then be defined as:

E.g:  typedef struct {int id_no;

                      char name[40];

                      char dept[40];

                      int no_of_books;

                      USER_STATUS status;

                     } USER_RECORD;

·        The variable declaration then becomes:

USER_RECORD user;

·        Once a variable is declared, we can access the individual elements (called fields) of the record using the dot (.) operator.

For example, the following program reads and prints values for the structure variable user:

main()

{  USER_RECORD user;

   user.no_of_books=0;

   printf("Enter user details:\n");

   printf("ID_NO: ");  scanf("%d",&user.id_no);

   printf("NAME: ");  fflush(stdin); gets(user.name);

   printf("DEPARTMENT: ");  gets(user.dept);

   printf("STATUS[0 for student,1 for staff,2 for faculty]");

   scanf("%d",&user.status);

 

   printf("\nThe details you entered are:\n");

   printf("ID_NO:      %d\n", user.id_no);

   printf("NAME:       %s\n", user.name);

   printf("DEPARTMENT: %s\n", user.dept);

   printf("STATUS:     %d\n", user.status);

   printf("NO OF BOOKS %d\n", user.no_of_books);

   return 0;

}

Nested (or compound) Structure:

A structure can have as its field another structure.

E.g.   typedef struct { int day;

                        int month;

                        int year;

                      } DATE;

       typedef struct { int bk_no;

                        char author[40];

                        char title[40];

                        char publisher[40];

                        DATE pub_date;

                     } BOOK_RECORD;

 

      BOOK_RECORD book;

 

We then need two dot operators to access the publication date in the variable book

book.pub_date.day=2;

 

Array of structures:

·         We can also have an array of structures. For example:  BOOK_RECORD books[100];

·         Assignment to the bk_no field of the first book in the array can be done as: Books[0].bk_no=3333;

 

Assignment and Comparison among structures:

·         It is allowed to assign a structure to another structure of the same typ as:   book2 = book1.

·        However, comparison is not allowed on entire structures.  It has to be done on field by field basis.

 

Structures as output parameters and the -> operator.

·        Structures can  be passed as output parameters in a manner similar to simple variables, using the * operator.

void  update_records( BOOK_RECORD *book, USER_RECORD *user)

{   (*book).status = BORROWED;

    (*user).no_of_books++;

}

·        In the above example, the * is necessary since book in the function is a pointer variable, so we need to dereference it to  access the object it is pointing to.

·        The bracket in (*book).status is also necessary since the dot operator has high priority than *.

·        Another way to represent above statement is by using the arrow operator (->).  E.g.

book -> status;  is equivalent to   (*book).status;

book->pub_date.day; is equivalent to (*book).pub_date.day;

 

Programming Exercise:

1.   The program, LAB_1.C declares a structure for a student record consisting of the fields:

id_no (long),name,exam1,exam2,exam3 and average.

The program then uses functions: get_record to read data into the a record, get_average to computes the average and print_record to print the whole record.  Open the program, study it and test-run it.

 

2.      Modify the program in (1) so the function get_record uses return statement instead of pointer parameter.

 

3.      Modify program (2) so that the data (one record) is read from the  file EXAM2.DAT by writing another function fget_record which reads the record from file.

Hint:          you need to pass a FILE pointer to the functions fget_record  to read a record from it.

you should use fgets to read the name – assume that the name is not more than 17 characters.

 

4.   Modify the program in (2) to process all the records in the EXAM2.DAT file. (assuming the number of records is not known).  You need to add another function print_horizontal that prints a record on one line.

 

Home Work:

The file ATTEND.DAT  contains an unknown number of ID’s of students and their attendances in 20 lectures where 1 represents attendance and 0 represents absence.  Write a program that reads the content of the file into an array of structure, count the number of absences for each student and print the records horizontally on the screen.  You need to declares a structure consisting of three fields: ID (long int); attend, an int array of size 20; and absent, a field to store total the number of absences.   The program should have at least three functions: fget_record, count_absents, and print_record