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.

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

Program 11.14<br />

Output<br />

A str<strong>in</strong>g to be copied.<br />

So is this.<br />

Operations on Po<strong>in</strong>ters<br />

As you have seen <strong>in</strong> this chapter, you can add or subtract <strong>in</strong>teger values from po<strong>in</strong>ters.<br />

Furthermore, you can compare two po<strong>in</strong>ters to see if they are equal or not, or if one<br />

po<strong>in</strong>ter is less than or greater than another po<strong>in</strong>ter.The only other operation that is permitted<br />

on po<strong>in</strong>ters is the subtraction of two po<strong>in</strong>ters of the same type.The result of subtract<strong>in</strong>g<br />

two po<strong>in</strong>ters <strong>in</strong> C is the number of elements conta<strong>in</strong>ed between the two po<strong>in</strong>ters.<br />

So, if a po<strong>in</strong>ts to an array of elements of any type and b po<strong>in</strong>ts to another element<br />

somewhere farther along <strong>in</strong> the same array, the expression b – a represents the number<br />

of elements between these two po<strong>in</strong>ters. For example, if p po<strong>in</strong>ts to some element <strong>in</strong> an<br />

array x, the statement<br />

n = p - x;<br />

has the effect of assign<strong>in</strong>g to the variable n (assumed here to be an <strong>in</strong>teger variable) the<br />

<strong>in</strong>dex number of the element <strong>in</strong>side x to which p po<strong>in</strong>ts. 4 Therefore, if p is set po<strong>in</strong>t<strong>in</strong>g<br />

to the hundredth element <strong>in</strong> x by a statement such as<br />

p = &x[99];<br />

the value of n after the previous subtraction is performed is 99.<br />

As a practical application of this newly learned fact about po<strong>in</strong>ter subtraction, take a<br />

look at a new version of the str<strong>in</strong>gLength function from Chapter 10.<br />

In Program 11.15, the character po<strong>in</strong>ter cptr is used to sequence through the characters<br />

po<strong>in</strong>ted to by str<strong>in</strong>g until the null character is reached. At that po<strong>in</strong>t, str<strong>in</strong>g is<br />

subtracted from cptr to obta<strong>in</strong> the number of elements (characters) conta<strong>in</strong>ed <strong>in</strong> the<br />

str<strong>in</strong>g. The program’s output verifies that the function is work<strong>in</strong>g correctly.<br />

Program 11.15 Us<strong>in</strong>g Po<strong>in</strong>ters to F<strong>in</strong>d the Length of a Str<strong>in</strong>g<br />

// Function to count the characters <strong>in</strong> a str<strong>in</strong>g – Po<strong>in</strong>ter version<br />

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

<strong>in</strong>t str<strong>in</strong>gLength (const char *str<strong>in</strong>g)<br />

{<br />

const char *cptr = str<strong>in</strong>g;<br />

4.The actual type of signed <strong>in</strong>teger that is produced by subtract<strong>in</strong>g two po<strong>in</strong>ters (for example,<br />

<strong>in</strong>t, long <strong>in</strong>t, or long long <strong>in</strong>t) is ptrdiff_t, which is def<strong>in</strong>ed <strong>in</strong> the standard header file<br />

.

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

Saved successfully!

Ooh no, something went wrong!