20.09.2015 Views

Programming in C

Kochan - ProgramminginC

Kochan - ProgramminginC

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Work<strong>in</strong>g with Po<strong>in</strong>ters and Structures<br />

245<br />

po<strong>in</strong>ters<br />

p1<br />

p2<br />

i1<br />

100<br />

i2<br />

-97<br />

Figure 11.3<br />

Structure conta<strong>in</strong><strong>in</strong>g po<strong>in</strong>ters.<br />

This def<strong>in</strong>es a structure called entry, which conta<strong>in</strong>s two members.The first member of<br />

the structure is a simple <strong>in</strong>teger called value.The second member of the structure is a<br />

member called next, which is a po<strong>in</strong>ter to an entry structure.Th<strong>in</strong>k about this for a<br />

moment. Conta<strong>in</strong>ed <strong>in</strong>side an entry structure is a po<strong>in</strong>ter to another entry structure.<br />

This is a perfectly valid concept <strong>in</strong> the C language. Now suppose you def<strong>in</strong>e two variables<br />

to be of type struct entry as follows:<br />

struct entry n1, n2;<br />

You set the next po<strong>in</strong>ter of structure n1 to po<strong>in</strong>t to structure n2 by execut<strong>in</strong>g the follow<strong>in</strong>g<br />

statement:<br />

n1.next = &n2;<br />

This statement effectively makes a l<strong>in</strong>k between n1 and n2, as depicted <strong>in</strong> Figure 11.4.<br />

n1<br />

value<br />

next<br />

n2<br />

value<br />

next<br />

Figure 11.4<br />

L<strong>in</strong>ked structures.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!