************* 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 - 10


 STRUCTURES, ARRAYS AND POINTERS 

P1

Structures, Arrays ...|Topics to Learn|10-0|9,49
  #                    Topic
 ---                  -------
  1   -   The Relation of Structures and Arrays
  2   -   Arrays of Pointers to Structures
  3   -   Cast and Changing the Object's Data Type
  4   -   Pointers and Multidimensional Arrays
  5   -   Structures and the Standard I/O
  6   -   Lesson Review
  0   -   Return to the Main Menu

P2

Structures, Arrays ...|Structures and Arrays|10-1.1|12,54
     A structure may contain array(s) as one or 
more of its elements.  The array at the same time 
may contain several structures, all of identical type.

The following is an example of an array of pointers 
to structures.  Initially we create a user-defined 
data type, bank_service and then we use bank_service 
to declare a variable valid_t_tab which may hold 
pointers/addresses to/of up to 25 variables of type 
bank_service.  The variable valid_t_tab represents 
the valid transaction table holding the records for 
the valid transactions.

P3

Structures, Arrays ...|Structures and Arrays|10-1.2|10,46
    /*  Array of pointers to structures */

typedef struct valid_code {
    char trans_code[8+1]; /* transaction code*/
    char prog_name[64];         /* task name */
} bank_service;
bank_service *valid_t_tab[25];

Declaration of an array of 25 pointers to
banking service structures.

P4

Structures, Arrays ...|Arrays of Pointers to Structures|10-2.1|7,54
The following example shows the usage of an array of 
pointers to structures, acct_ar.  The important points 
to have in mind are how the array is being indexed 
and the fact that we are dealing with a pointer to 
a structure rather than an explicit structure variable.  
Also note that the C function sizeof() will determine
the argument size in bytes.

P5

Structures, Arrays ...|Arrays of Pointers to Structures|10-2.2|17,54
/* On a typical computer one must supply a user ID and 
   a password in order to log on.  The data on valid 
   accounts is kept in memory in the array acct_ar, which 
   may hold up to 200 account records.                */

          typedef struct{
          char user_id[21];                /* user id */
          char password[16];    /* encrypted password */
          long date;       /* encoded expiration date */
          } account;

/* The records are sorted using the user ID as the key 
   and are stored in the array in ascending order.  
   Below is the function that will search the array for 
   a matching account and return TRUE (1) if an account 
   exists, and FALSE (0) if it doesn't.  The search 
   algorithm used is binary search                   */

P6

Structures, Arrays ...|Arrays of Pointers to Structures|10-2.3|15,51
#define TRUE    1
#define FALSE   0
#define ACCT_SIZE sizeof (account)
account *acct_ar[200];  /*array of pointers to 
                           user account records */
validate(acct_rec, n)
  account *acct_rec;  /* pointer to user's acct */
  int n;                  /* number of accounts */
{
  int  low, high, mid, cond;

  low =0;
  high = n-1;
  while (low <=high) {
      mid =(low + high) / 2;

P7

Structures, Arrays ...|Arrays of Pointers to Structures|10-2.4|17,50
     /* Compare the entered account to 
              the current account record in array */
     if ((cond=strcmp(acct_ar[mid] ->user_id,
          acct_rec->user_id)) < 0)
          high = mid -1;
     else if (cond > 0)
          low = mid +1;
     else {    /*check if password and date match */
          if ((strcmp (acct_ar[mid]->password,
              acct_rec->password) ==0) &&
              acct_ar[mid]->date >=acct_rec->date)
                 return(TRUE);
          else return(FALSE);         /* no match */
     }
   }      /* user_id not found, return error code */
   return(FALSE);
}

P8

Structures, Arrays ...|Casting|10-3.1|15,54
     In several situations it is required to
allocate memory for objects of type other than char.


     The function malloc() returns a pointer 
to a character string.


     A C cast forces a variable of one type, into 
a variable of another type:

               pointer to a character --> into a
               pointer to a structure

               float --> into an int

P9

Structures, Arrays ...|Casting|10-3.2|9,54
      The syntax of cast is:

             (data type) expression

where data type is the type you want


      Compilers perform standard conversions on 
expressions to the type specified by the data type.

P10

Structures, Arrays ...|Casting|10-3.3|9,54
     The following example shows one way to implement
a special type of memory allocating function.  
The C library function malloc() returns a pointer 
to a variable of type char.  We need to change 
that type.  The type conversion is accomplished using 
the C cast, in this case bank_rec * which is a
cast into a pointer to the type bank_rec.  Note also 
that the function serv_alloc() will also return a 
pointer to a variable of type bank_rec .

P11

Structures, Arrays ...|Casting|10-3.4|15,54
/* Converting a pointer to a character string
                  into a pointer to structure */
typedef  struct valid_code{
   char trans_code[9];
   char prog_name[64];
} bank_rec;

bank_rec *serv_alloc() /* return a pointer
                              to a structure */
{
   char *malloc();  /* returns non - int */
   return ((bank_rec*) malloc(sizeof(bank_rec))));
}

Note:  sizeof() returns the size of the object in bytes.

P12

Structures, Arrays ...|Pointers and Multidimensional Arrays|10-4.1|17,58
     Consider the declarations:

            int ar_int[5][15];    (1)
            int *ar_ptr[5];       (2)

Declaration (1) explicitly allocates 5 X 15 integer 
locations.  Declaration (2) allocates five locations
to store up to 5 addresses of integer arrays, perhaps 
of various lengths.

     These arrays may or may not exist.

     Advantages of using an array of pointers:

 *  The accessing is done by indirection through a pointer, 
    and not by multiplication and addition.
 *  The rows of the array may be of different lengths.

P13

Structures, Arrays ...|Structures and Standard I/O|10-5.1|17,61
/* Software Search Agency Description */

A software search agency specializes in the placement of
software professionals into career positions.  The agency
keeps records on the professionals who are seeking new
positions, and matches those records with the requests that
come in from the local clients (employers).  The personal 
information is kept in a text file in the following format:

   columns 1-50     name and telephone number
   columns 51-52    salary requirement
   columns 53-54    years of experience
   columns 55-60    areas of specialization, a particular
                    column is marked by an X if it applies
                    (data bases, operating systems, graphics,
                    business application, languages, project
                    management)

P14

Structures, Arrays ...|Structures and Standard I/O|10-5.2|12,54
Software Search Agency Description (continued ...)

The requests from employers come in a similar format;
however, the name and the telephone number are obviously
not included.  (Shown are a few possible lines of the
input.)

123456789012345678901234567890123456789012345667890123

John Forsyth      (213)-867-8459           4209 X X X
Anne Blakeley     (202)-207-4281           3706 X X
William Bronson   (617)-659-7794           5213 XX X

P15

Structures, Arrays ...|Structures and Standard I/O|10-5.3|18,54
Software Search Agency Description (continued ...)

The program must read the request on the first line in 
the file, followed by the lines representing the available 
personnel.  The program must determine if there is a person 
matching the specified request.

The stipulations for the match are:
   1:   The salary offered should not be lower by more
        than $3K/year than the salary requested.
   2:   The number of years of experience specified
        should not be greater by more than two than
        the number of years of candidate's experience.
   3:   There must be a match in the specialty requested
        with one of the candidate's specialities.

Finally, the program must print the name and the
telephone number of the potential candidates.     */

P16

Structures, Arrays ...|Structures and Standard I/O|10-5.4|17,52
Software Search Agency Program 
#include <stdio.h>
/* External Declarations */
struct candidate {
   char id[51];                /* name and tel.no */
   short salary                  /* salary in $Ks */
   short experience;       /* years of experience */
   char specialities[6];   /* areas of speciality */
};
/* -----------------------------------------------*/
main ()  {    struct candidate request, person;
   printf("all following candidates meet");
   if ((read_client(&request) != EOF))
       while ((read_client(&person) != EOF))
           if (compatible(&request, &person))
               printf("%s\n, person.id);
}

P17

Structures, Arrays ...|Structures and Standard I/O|10-5.5|16,53
Software Search Agency Program (continued)

read_client(client)
     struct candidate *client;
{
   char *gets(), s[81];
   int  i,c;

   /* read identity                         */
   i = 0;
   while (((c=client->id[i++] = getchar()) != EOF) &&
        (i < 50));
   if (c != EOF) {/* then there is a full record */
         client->id[50] = ' ';
   /* read client's salary */
   scanf("%2d", &client-> salary);

P18

Structures, Arrays ...|Structures and Standard I/O|10-5.6|13,43
Software Search Agency Program (continued)

    /* READ YEARS OF EXPERIENCE    */
    scanf("%d", &client->experience);
    /* READ IN CLIENT'S SPECIALITIES */
    for (i =0; i < 6; i++)
       client-> specialities[i]= getchar();
       gets(s);
       /* RETURN O.K. CODE     */
       return(1);
   }
   return(EOF);
}

P19

Structures, Arrays ...|Structures and Standard I/O|10-5.7|18,54
Software Search Agency Program (continued)
#define TRUE  1
#define FALSE 0
   compatible (request, client)
          struct  candidate *request, *client;
   {    int compat=FALSE, i;
      if (((client->salary - request-> salary) <=3) &&
         ((request -> experience -
                         client->experience) <=2)){
         for (i=0; i<6; i++)
           if (request->specialities[i] == 'X' &&
              client-> specialities[i] == 'X'){
                 compat = TRUE;
                 break;
      }
   }
   return compat;
}

P20

Structures, Arrays ...|Structures and Standard I/O|10-5.8|15,55
Software Search Agency Program (continued)
 /*  With the following input  */
 Request                                     4810 X
 Alex Anderson        (818)  279-1436        4408 X X X
 Alice Jordan         (202)  347-9245        3604 XX
 Michele Hopkins      (213)  319-3400        5214 X X X
 John Holdridge       (617)  236-1746        4711 X X X
 Judy Boston          (619)  362-9867        4509 X X
 Richard Wilson       (408)  988-5249        4810 XX

/*  The following output is produced  */
The following candidates meet the requirements

Alex Anderson          (818)  279-1436
Judy Boston            (619)  362-9867