L-22 MCS 275 Wed 28 Feb 2001

This is the file address.h :

struct city
{
   int  zip;
   char *name;
};

print_city ( struct city c );
/* writes zip code and the name of the city */

struct person
{
   char *name;
   int key;         /* key to the door of home of person */
};

struct home
{
   int door;        /* door opens to some key */
   char *street;    /* street name */
   int strnum,zip;  /* street number and zip code */
};

print_address ( struct person p, struct home h, struct city c );
/* prints the address of the person */ 

struct homeaddress  /* collects home and city */
{
   struct home h;
   struct city c;
};

int find_home ( int key, int n, struct home homes[], struct home *h);
/* Returns the index of homes[index], for which the door matches
   with the given key, in that case h = homes[index].
   If there is no home whose door matches the key, -1 is returned. */
and here is the file address.c :
/* L-22 MCS 275 Wed 28 Feb 2001 addresses */

#include <stdio.h>
#include <address.h>  /* needs to compile with gcc -I. address.c */
                            /* alternative is #include "address.h" */

int main()
{
   struct city uic;
   struct person bob = { "Bob", 10 }; 
   struct person mary = { "Mary", 10 };
   struct person bill = { "Bill" , 20 };
   struct person anna = { "Anna" , 30 };
   struct home bobsplace = { 10 , "15th ST", 321 , 60607 };
   struct home annasplace = { 30 , "10th ST", 121 , 60607 };
   struct home billsplace = { 20 , "22nd ST", 542 , 60607 };
   struct homeaddress bobsaddress;
   struct home h;
   struct home homes[3];
   int key,i;

   homes[0] = bobsplace;
   homes[1] = annasplace;
   homes[2] = billsplace;

   uic.zip = 60607;
   uic.name = "Chicago";
   printf("our city is ");
   print_city(uic);
   printf("\n");

   printf("a couple of addressses : \n");
   print_address(bob,bobsplace,uic);
   print_address(mary,bobsplace,uic);

   bobsaddress.h = bobsplace;
   bobsaddress.c = uic;

   printf("Here is Bob's address again :\n");
   print_address(bob,bobsaddress.h,bobsaddress.c);

   homes[0] = bobsplace;
   homes[1] = annasplace;
   homes[2] = billsplace;
   printf("The street names with door keys of homes  : \n");
   for ( i = 0; i <= 2; i++ )
      printf("%s with door key %d \n", homes[i].street, homes[i].door);

   printf("Give a key : "); scanf("%d", &key);
   i = find_home(key,2,homes,&h);
   if (i >= 0)
      printf("found home %s %d at index %d \n", h.street, h.strnum, i);
   else
      printf("home with key %d not found.\n, key");

   return 0;
}


print_city ( struct city c )
{
   printf("%d %s", c.zip, c.name);
}

print_address ( struct person p, struct home h, struct city c )
{
   printf("%10s, %s %d, %d %s\n", p.name, h.street, h.strnum, c.zip, c.name);
}

int find_home ( int key, int n, struct home homes[], struct home *h)
{
   int i;

   for (i = 0; i <= n ; i++)
      if (homes[i].door == key)
      {
         h->street = homes[i].street;
         h->strnum = homes[i].strnum;
         h->zip = homes[i].zip;
         /* alternative : (*h).zip = homes[i].zip; NEEDS BRACKETS */
         return i;
      }

   return -1;
}