************* BACK AND FORTH PAGING SUBROUTINE ************

PAGEFLOP

If they want to quit, do so
 If They want to quit subsession, Do SO!
  Decrement LOCAL and GLOBAL Page Counters

FORWARD

  Increment LOCAL and GLOBAL Page Counters
   *************** END PAGEFLOP *********************
  ****** CHAPTER INTERNAL SELECTOR ALGORITHM ******

SELECTOR

   Reset Local Page Counter

SELAGAIN

 CHECK IF ONLY A <CR>

WRONGSEL

YOUR SELECTION IS NOT IN THE 0 - #SELIMIT  RANGE  Please try again

AANVANG

Copyright CourseWare Technologies Inc., 1985-88


Lesson - 9


 C STRUCTURES AND RECORDS 

P1

Structures|Topics to Learn|9-0|12,47
   #                   Topic
  ---                 -------
   1   -   The Structure Concept
   2   -   Structure Definition and Declaration
   3   -   Structure Initialization
   4   -   Using Structures
   5   -   Structure Nesting
   6   -   Structures as Arguments
   7   -   User Defined Data Types
   8   -   Operator Hierarchy
   9   -   Lesson Review
   0   -   Return to the Main Menu

P2

Structures|The Structure Concept|9-1.1|15,54
     A structure in C is a collection of one or more
(usually two or more) variables of differing data 
types grouped under the same name.  The prefix struct 
is used to create a structure.


     C has the following data types:

      Unstructured:  int, char, float, pointer

        Structured:

            Homogeneous:  arrays

          Heterogeneous:  structures, unions

P3

Structures|Structure Definition & Declaration|9-2.1|14,54
The following example shows a declaration of the structure 
employee which says that the employee record consists 
of employee number, empl_number, employee name, empl_name
and employee salary, salary.


              /* typical logically related data 
                 represented by a structure */

              struct employee {
                  int empl_number;
                  char empl_name[25];
                  float salary;
              }

P4

Structures|Structure Definition & Declaration|9-2.2|8,54
     In the previous example, employee is a structure 
tag and is used for future structure declarations, 
however it is not a variable, that is, it reserves no 
accessible memory space.


     Structure definition creates a template in memory 
that is not accessible.  (See the following example.)

P5

Structures|Structure Definition and Declaration|9-2.3|13, 54

       struct postage {
            int class;       /* mail class */
            float weight; /* object weight */
            int region;
            float mail_cost;
        }

If int occupies 2 bytes, and float 4, then the array
may be represented as occupying 12 bytes:

      |class |weight      |region| mail_cost  |
      0      4            12     16           23

P6

Structures|Structure Definition & Declaration|9-2.4|16,52
An example from the above sample using the structure tag
to declare a structure variable stamp_cost, 
i.e., struct postage stamp_cost.

This says that the variable stamp_cost is of type 
postage and consists of four members: class, weight, 
region, and mail_cost.

           -------- EQUIVALENTLY  -------
           
                  struct postage {
                      int class;
                      float weight;
                      int region;
                      float mailcost;
                  } stamp_cost;

P7

Structures|Structure Initialization|9-3.1|18,54
     A structure may be initialized if it is declared 
as external or static.  The initialization is done 
by following a structure variable with a list of 
initializers.  See the following example.

/* Using the Structure Definition */

    struct employee programmer =
               { 767, "John Sanders"};
    
NOTE:  salary was initialized to zero.
    
    ---------------- EQUIVALENTLY ----------------
    struct {
        int empl_number;
        char empl_name[25];
        float salary;
    } programmer = { 767, "John Sanders"};

P8

Structures|Using C Structures|9-4.1|15,54
     To access a single element of a structure, one 
needs to specify the structure variable followed by 
 .  and the element of the structure.

       /* Accessing structure elements, */
       
       programmer.emp1_number = 4837;
       
       /* Comparing elements of a structure 
          to a character string */
       
       if (strcmp(programmer.empl_name, "M. Deutsch")
                            == 0)
            printf("Strings match\n");
       else
            printf("Strings Do Not Match\n");

P9

Structures|Using C Structures|9-4.2|12,51
The following example shows one way to implement
the string compare function strcmp(), which is 
also a part of the C library.  Note the delimiting 
condition is either when the end of the string s2 
is reached ('\0'), or when the current character of 
s1 is not equal to the current character of s2.
If the integer value of the character in s1 at the
terminating condition is greater than that of the
character in s2, then a value > 0 will be returned.
If the integer value of the character in s1 at the
terminating condition is smaller than that of the 
character in s2, then a value < 0 will be returned.

P10

Structures|Using C Structures|9-4.3|13,40
strcmp(s1,s2)    /* returns < 0 if s1 < s2,
                    > 0 if s1 > s2, and 0 if
                   both strings are equal */

     char *s1, *s2;
{
     while (*s1 == *s2)    {
         if (*s2 == '\0')
             return(0);
         else { s1++; s2++; }
     }
     return(*s1 - *s2);
}

P11

Structures|Structure Nesting|9-5.1|16,54
     C structures may be nested, that is, one structure
may have another structure as one of its elements.

The following is an example of nested structures.
The structure tag struct personnel we have seen
already; the structure with the structure tag 
struct soft_project is made up of four elements: 
months, the length of the project, emp_num, the number 
of employees, budget the budget for the project, 
and an array of nested structures, employees. The 
element employees occupies the memory space required 
by 55 variables of type struct personnel, which is 
a serious consideration.  Such a large memory 
requirement would be logical if data on several 
objects had to be stored in a file and retrieved 
in an efficient manner.

P12

Structures|Structure Nesting|9-5.2|14,47
/* Personnel Structure */

struct personnel {
    int empl_number;
    char empl_name[25];
    float salary;
};
/* ----- software project structure ---- */
struct soft_project {
    int months;
    int emp_num;  /* number of employees */
    double budget;
    struct personnel employees[55];
} project;

P13

Structures|Structure Nesting|9-5.3|13,54
     Referencing elements of a nested structure:

To reference the employee name of the 27th employee use:

     strcpy(project.employees[27].empl_name,
                       "Victor Programmer");

Where strcpy() may be written as shown in the
following example.  Note that the delimiting 
condition occurs when the source string pointer
sourc_str points to the NULL character.  Then
the expression inside the while statement becomes 
FALSE, i.e. evaluates to zero.

P14

Structures|Structure Nesting|9-5.4|5,37
strcpy(dest_str, sourc_str)
    char *dest_str, *sourc_str;
{
  while (*dest_str++ = *sourc_str++);
}

P15

Structures|Structures as Arguments|9-6.1| 18 , 50
     A structure is not an array; therefore, when 
a structure name appears in the argument list, the
structure, not its address, is being passed.


     To pass a structure address, one must specify
a variable as a pointer to a structure.


     The elements of a pointer to a structure are
referenced by following the structure pointer 
variable with  ->  rather than with  .  as in the
case with explicit structure variables, i.e:

      struct soft_project *proj_ptr;
                  :
                  :
      proj_ptr->employees[27].empl_name;

P16

Structures|Structures as Arguments|9-6.2|9,54
     Defining a banking service record:

    struct bank_service {
        char trans_code[8+1];  /* service name */
        char prog_name[64];  /* name of program
                    to process the transaction */
        char *description;   /* transaction
                               description     */
    };

P17

Structures|User Defined Data Types|9-7.1|11,54
     A shorthand for data representation is the facility 
typedef.   Whenever typedef precedes a variable 
declaration, the variable name itself may be used
later to define data types rather than rewriting 
all the required types.  For example:

      typedef char *string; /* pointer to a character
                                   string         */
      typedef char file_name[64]; /* program name */

      typedef char id[8+1];    /* identifier/name */

P18

Structures|User Defined Data Types|9-7.2|14,54
     Using the user-defined types from the previous 
screen, we can redefine the structure as follows:

            typedef struct valid_code {
                id trans_code;
                file_name prog_name;
                string description;
            } bank_service;


     NOTE that this example created two tags to define 
a variable of the same type, these tags are:

       struct valid_code  and  bank_service;

P19

Structures|Operator Hierarchy|9-8.1|18,54
     In the expressions dealing with pointers or
in complicated expressions, it is important to 
know the operator precedence hierarchy:

        OPERATOR             ASSOCIATIVITY
        --------             -------------
        () [] -> .           left to right
        !~ ++ -- - * &       right to left
        * / %                left to right
        + -                       |
        << >>
        &                         |
        ^
        |                         |
        &&                   left to right
        ||                   right to left
        ?:                   right to left
        ,                    left to right