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.

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

sets px to po<strong>in</strong>t n elements farther <strong>in</strong> the array, no matter what type of element is conta<strong>in</strong>ed <strong>in</strong><br />

the array.<br />

The <strong>in</strong>crement and decrement operators ++ and -- are particularly handy when deal<strong>in</strong>g<br />

with po<strong>in</strong>ters. Apply<strong>in</strong>g the <strong>in</strong>crement operator to a po<strong>in</strong>ter has the same effect as<br />

add<strong>in</strong>g one to the po<strong>in</strong>ter, while apply<strong>in</strong>g the decrement operator has the same effect as<br />

subtract<strong>in</strong>g one from the po<strong>in</strong>ter. So, if textPtr is def<strong>in</strong>ed as a char po<strong>in</strong>ter and is set<br />

po<strong>in</strong>t<strong>in</strong>g to the beg<strong>in</strong>n<strong>in</strong>g of an array of chars called text, the statement<br />

++textPtr;<br />

sets textPtr po<strong>in</strong>t<strong>in</strong>g to the next character <strong>in</strong> text, which is text[1]. In a similar fashion,<br />

the statement<br />

--textPtr;<br />

sets textPtr po<strong>in</strong>t<strong>in</strong>g to the previous character <strong>in</strong> text, assum<strong>in</strong>g, of course, that<br />

textPtr was not po<strong>in</strong>t<strong>in</strong>g to the beg<strong>in</strong>n<strong>in</strong>g of text prior to the execution of this statement.<br />

It is perfectly valid to compare two po<strong>in</strong>ter variables <strong>in</strong> C.This is particularly useful<br />

when compar<strong>in</strong>g two po<strong>in</strong>ters <strong>in</strong> the same array. For example, you can test the po<strong>in</strong>ter<br />

valuesPtr to see if it po<strong>in</strong>ts past the end of an array conta<strong>in</strong><strong>in</strong>g 100 elements by compar<strong>in</strong>g<br />

it to a po<strong>in</strong>ter to the last element <strong>in</strong> the array. So, the expression<br />

valuesPtr > &values[99]<br />

is TRUE (nonzero) if valuesPtr is po<strong>in</strong>t<strong>in</strong>g past the last element <strong>in</strong> the values array,<br />

and is FALSE (zero) otherwise. Recall from previous discussions that you can replace the<br />

preced<strong>in</strong>g expression with its equivalent<br />

valuesPtr > values + 99<br />

because values used without a subscript is a po<strong>in</strong>ter to the beg<strong>in</strong>n<strong>in</strong>g of the values<br />

array. (Remember, it’s the same as writ<strong>in</strong>g &values[0].)<br />

Program 11.11 illustrates po<strong>in</strong>ters to arrays.The arraySum function calculates the sum<br />

of the elements conta<strong>in</strong>ed <strong>in</strong> an array of <strong>in</strong>tegers.<br />

Program 11.11 Work<strong>in</strong>g with Po<strong>in</strong>ters to Arrays<br />

// Function to sum the elements of an <strong>in</strong>teger array<br />

#<strong>in</strong>clude <br />

<strong>in</strong>t arraySum (<strong>in</strong>t array[], const <strong>in</strong>t n)<br />

{<br />

<strong>in</strong>t sum = 0, *ptr;<br />

<strong>in</strong>t * const arrayEnd = array + n;<br />

for ( ptr = array; ptr < arrayEnd; ++ptr )<br />

sum += *ptr;

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

Saved successfully!

Ooh no, something went wrong!