Another example is a person record type. This is shown below using Pascal
syntax.
struct person {
int id;
char bday[10];
struct address_record address;
struct person so;
}
person is the record type name. It is a type in the same sense
as integer is a type. It is not a variable.
person joe; { ok }
person staff[10]; { ok }
person = 6; { no }
person.id = 6; { no }
Using person as a variable is like saying
integer = 6
The syntax for selecting a field from a record variable is
variable.field_name
This can be used on either side of the assignment statement.
person joe;
joe.id = 42;
printf("Birthdate is %s\n",joe.bday);
joesid = joe.id;
id | integer | 2 bytes |
bday | string | 9 bytes |
address | address_record | 60 bytes |
so | person | ? bytes |
This makes finger a variable of type "pointer to a person record". It
is not a variable of type person.
finger.id = 42;
This is meaningless since a pointer variable takes up only enough space
as needed for an address. Thus there is no room for the fields. To select
a field when given a pointer to a record,
finger->id = 42;
The type of finger is "pointer to person". The type of finger-> is "person".
int foo[4][2];To help with some of the notation, here are a couple of defines.
#define VALUE 0 /* the column number of the value */ #define NEXT 1 /* the column number of the pointer */After a few insertions, the tables looks like this:
0 | 0 |
27 | 3 |
52 | 0 |
105 | 2 |
void print_list(int foo[][],int list) { int ptr; ptr = list; while(foo[ptr][NEXT] != 0) { printf("Value: %d\n",foo[ptr][VALUE]); ptr=foo[ptr][NEXT]; } /* while */ } /* print_list */
list=1; print_list(foo,list); 27 105
while(foo[ptr][NEXT] != 0) {with:
while(ptr != 0) {
function addchar(L,C)
// This adds a character to the end of list L
1. new(fresh)
// make a new record
2. fresh->value = C
3. fresh->next = NULL
4. if (L != NULL) {
// find the end of the list
5.
x = L
6.
while (x->next != NULL) {
7.
x = x->next
8.
} // while
9.
x->next = fresh // append fresh
}
10. else {
11.
L = fresh // This is the first element
12. }
end addchar
procedure insertchar(Str,C)
// This adds an element
to the list Str. Str represents one
// string. The element is
added to the front of the list
1.
new(fresh) // make a new record
// fill it in
2.
fresh->value = C;
// now point it at
the front of the list
3.
fresh->next = Str;
4.
Str = fresh; // point the front of the list at
fresh
end insertchar
function findchar(L,C)
// Search the list L for the character C and
return a pointer
// to it and return NIL if it is not found
1. x = L;
2. while (x != NULL)
and (x->value != C) {
3.
x = x->next
4. } // while
// here, either x ==
NULL, meaning we didn't find it or it points to the right record.
So return x
5. return(x);
end findchar
function delete(L,C)
// remove the character C from the list
1. if (L->value == C)
// special case of first one
2. L = L->next
3. else
4. before = L
// the previous one
5. current =
L->next // the one we are looking at
6. while (current
!= NULL) {
7.
if (current->value == C) { // point before at next one
8.
before->next = current->next
9.
else { // move both pointers ahead
10.
before = current;
11.
current = current->next;
12.
}
13. } // while
14. }
end delete
.fi