11.07.2015 Views

tYSR20

tYSR20

tYSR20

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.

Chapter 9: Taking a Second Look at C++ Pointers 127Table 9-2Adding OffsetsOffset Result Is the Address of+ 0 0x110 charArray[0]+ 1 0x111 charArray[1]+ 2 0x112 charArray[2]... ... ...+ n 0x110 + n charArray[n]The addition of an offset to a pointer is similar to applying an index to anarray.Thus, ifchar* ptr = &charArray[0];then*(ptr + n) ← corresponds with → charArray[n]Because * has higher precedence than addition, * ptr + n adds n to thecharacter that ptr points to. The parentheses are needed to force the additionto occur before the indirection. The expression *(ptr + n) retrievesthe character pointed at by the pointer ptr plus the offset n.In fact, the correspondence between the two forms of expression is so strongthat C++ considers array[n] nothing more than a simplified version of *(ptr+ n), where ptr points to the first element in array.array[n] -- C++ interprets as → *(&array[0] + n)In order to complete the association, C++ takes a second shortcut. If givenchar charArray[20];charArray is defined as &charArray[0];.That is, the name of an array without a subscript present is the address ofthe array itself. Thus, you can further simplify the association toarray[n] --> C++ interprets as --> *(array + n)

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

Saved successfully!

Ooh no, something went wrong!