Programming in C

(Barry) #1
Working with Pointers and Structures 245

Figure 11.3 Structure containing pointers.

This defines a structure called entry, which contains two members.The first member of
the structure is a simple integer called value.The second member of the structure is a
member called next, which is a pointer to an entrystructure.Think about this for a
moment. Contained inside an entrystructure is a pointer to another entrystructure.
This is a perfectly valid concept in the C language. Now suppose you define two vari-
ables to be of type struct entryas follows:


struct entry n1, n2;


You set the next pointer of structure n1to point to structure n2by executing the fol-
lowing statement:


n1.next = &n2;


This statement effectively makes a link between n1and n2, as depicted in Figure 11.4.


pointers

p1
p2

100

-97

i1

i2

n1

value
next

n2

value
next

Figure 11.4 Linked structures.
Free download pdf