L-24 MCS 275 Mon 5 Mar 2001
Below are the listings of the program we discussed in class:
/* L-24 MCS 275 Mon 5 Mar 2001 cross-referential structures */
#include <stdio.h>
struct person
{
char *name;
int age;
struct home *h;
};
struct home
{
char *street;
int number,zip;
char *city;
struct person *p;
};
int main()
{
struct person bob;
struct home bobsplace;
bob.name = "Bob";
bob.age = 54;
bob.h = &bobsplace;
bobsplace.street = "12th ST";
bobsplace.number = 23;
bobsplace.city = "Chicago";
bobsplace.zip = 60607;
bobsplace.p = &bob;
printf("Street where %s lives is %s.\n", bob.name, bob.h->street );
printf("Name of person living in %s %d is %s.\n",
bobsplace.street, bobsplace.number, bobsplace.p->name);
return 0;
}
Here is the same program, but now with typedefs :
/* L-24 MCS 275 Mon 5 Mar 2001 cross-referential structures */
#include <stdio.h>
typedef struct person PERSON;
typedef struct home HOME;
struct person
{
char *name;
int age;
HOME *h;
};
struct home
{
char *street;
int number,zip;
char *city;
PERSON *p;
};
int main()
{
PERSON bob;
HOME bobsplace;
bob.name = "Bob";
bob.age = 54;
bob.h = &bobsplace;
bobsplace.street = "12th ST";
bobsplace.number = 23;
bobsplace.city = "Chicago";
bobsplace.zip = 60607;
bobsplace.p = &bob;
printf("Street where %s lives is %s.\n", bob.name, bob.h->street );
printf("Name of person living in %s %d is %s.\n",
bobsplace.street, bobsplace.number, bobsplace.p->name);
return 0;
}
And here is a modification, where the home contains pointers
to several people living there:
/* L-24 MCS 275 Mon 5 Mar 2001 cross-referential structures */
#include <stdio.h>
#include <stdlib.h>
typedef struct person PERSON;
typedef struct home HOME;
struct person
{
char *name;
int age;
HOME *h;
};
struct home
{
char *street;
int number,zip;
char *city;
int size;
PERSON *p;
};
int main()
{
struct person bob,anna;
struct home bobsplace;
int i;
bob.name = "Bob";
bob.age = 24;
bob.h = &bobsplace;
anna.name = "Anna";
anna.age = 23;
anna.h = &bobsplace;
bobsplace.street = "12th ST";
bobsplace.number = 23;
bobsplace.city = "Chicago";
bobsplace.zip = 60607;
bobsplace.size = 2;
bobsplace.p = (PERSON*)calloc(2,sizeof(PERSON));
bobsplace.p[0] = bob;
bobsplace.p[1] = anna;
printf("Street where %s lives is %s.\n", bob.name, bob.h->street );
printf("Numbers of persons living in %s %d : %d.\n",
bobsplace.street, bobsplace.number, bobsplace.size);
printf("Names of persons living there : \n");
for (i = 0; i < bobsplace.size; i++)
printf(" %s\n", bobsplace.p[i].name);
return 0;
}
All of this leads to self-referential structures:
/* L-24 MCS 275 Mon 5 Mar 2001 self-referential structures */
#include <stdio.h>
typedef struct list LIST;
struct list
{
int head;
LIST *tail;
};
int main()
{
LIST a,b,c;
a.head = 1; /* filling in the data */
b.head = 2;
c.head = 3;
printf("Elements in the list : %d %d %d.\n",
a.head, b.head, c.head);
a.tail = &b; /* making the links in the list */
b.tail = &c;
c.tail = NULL;
printf("Elements in the list : %d %d %d.\n",
a.head, a.tail->head, a.tail->tail->head );
return 0;
}