O'Reilly logo

C++ For Dummies®, 6th Edition by Stephen R. Davis

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 9. Taking a Second Look at C++ Pointers

In This Chapter

  • Performing arithmetic operations on character pointers

  • Examining the relationship between pointers and arrays

  • Increasing program performance

  • Extending pointer operations to different pointer types

  • Explaining the arguments to main() in our C++ program template

C++ allows the programmer to operate on pointer variables much as she would on simple types of variables. (The concept of pointer variables is introduced in Chapter 8.) How and why this is done along with its implications are the subjects of this chapter.

Defining Operations on Pointer Variables

Some of the same arithmetic operators I cover in Chapter 3 can be applied to pointer types. This section examines the implications of applying these operators to both to pointers and to the array types (I discuss arrays in Chapter 7). Table 9-1 lists the three fundamental operations that are defined on pointers. In Table 9-1, pointer, pointer1, and pointer2 are all of some pointer type, say char*; and offset is an integer, for example, long. C++ also supports the other operators related to addition and subtraction, such as ++ and +=., although they are not listed in Table 9-1.

Table 9.1. The Three Basic Operations Defined on Pointer Types

Operation

Result

Meaning

pointer + offset

pointer

Calculate the address of the object offset entries from pointer

pointer - offset

pointer

The opposite of addition

pointer2 - pointer1

offset

Calculate the number of entries between pointer2 and pointer1

The ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required