20.09.2015 Views

Programming in C

Kochan - ProgramminginC

Kochan - ProgramminginC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

244 Chapter 11 Po<strong>in</strong>ters<br />

Program 11.5<br />

}<br />

Cont<strong>in</strong>ued<br />

pr<strong>in</strong>tf ("i1 = %i, *po<strong>in</strong>ters.p1 = %i\n", i1, *po<strong>in</strong>ters.p1);<br />

pr<strong>in</strong>tf ("i2 = %i, *po<strong>in</strong>ters.p2 = %i\n", i2, *po<strong>in</strong>ters.p2);<br />

return 0;<br />

Program 11.5 Output<br />

i1 = 100, *po<strong>in</strong>ters.p1 = 100<br />

i2 = -97, *po<strong>in</strong>ters.p2 = -97<br />

After the variables have been def<strong>in</strong>ed, the assignment statement<br />

po<strong>in</strong>ters.p1 = &i1;<br />

sets the p1 member of po<strong>in</strong>ters po<strong>in</strong>t<strong>in</strong>g to the <strong>in</strong>teger variable i1, whereas the next<br />

statement<br />

po<strong>in</strong>ters.p2 = &i2;<br />

sets the p2 member po<strong>in</strong>t<strong>in</strong>g to i2. Next, –97 is assigned to the variable that is po<strong>in</strong>ted<br />

to by po<strong>in</strong>ters.p2. Because you just set this to po<strong>in</strong>t to i2, –97 is stored <strong>in</strong> i2.No<br />

parentheses are needed <strong>in</strong> this assignment statement because, as mentioned previously, the<br />

structure member operator . has higher precedence than the <strong>in</strong>direction operator.<br />

Therefore, the po<strong>in</strong>ter is correctly referenced from the structure before the <strong>in</strong>direction<br />

operator is applied. Of course, parentheses could have been used just to play it safe, as at<br />

times it can be difficult to try to remember which of two operators has higher precedence.<br />

The two pr<strong>in</strong>tf calls that follow each other <strong>in</strong> the preced<strong>in</strong>g program verify that the<br />

correct assignments were made.<br />

Figure 11.3 has been provided to help you understand the relationship between the<br />

variables i1, i2,and po<strong>in</strong>ters after the assignment statements from Program 11.5 have<br />

been executed. As you can see <strong>in</strong> Figure 11.3, the p1 member po<strong>in</strong>ts to the variable i1,<br />

which conta<strong>in</strong>s the value 100, whereas the p2 member po<strong>in</strong>ts to the variable i2, which<br />

conta<strong>in</strong>s the value –97.<br />

L<strong>in</strong>ked Lists<br />

The concepts of po<strong>in</strong>ters to structures and structures conta<strong>in</strong><strong>in</strong>g po<strong>in</strong>ters are very powerful<br />

ones <strong>in</strong> C, for they enable you to create sophisticated data structures, such as l<strong>in</strong>ked<br />

lists, doubly l<strong>in</strong>ked lists,and trees.<br />

Suppose you def<strong>in</strong>e a structure as follows:<br />

struct entry<br />

{<br />

<strong>in</strong>t<br />

struct entry<br />

};<br />

value;<br />

*next;

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

Saved successfully!

Ooh no, something went wrong!