Valid Pointer Operations

Here is a list of the operations that can properly be performed with pointers.

Creation The initial value of a pointer has three possible sources:

Assignment

Arithmetic

Comparison

Indirection

Indexing

The following bit of code in Example 22.6 demonstrates this last point rather clearly.

Example 22.6. src/arrays/pointerIndex.cpp

#include using namespace std; int main() { int x = 23; int* px = &x; cout << "px[0] = " << px[0] << endl; cout << "px[1] = " << px[1] << endl; cout << "px[-1] = " << px[-1] << endl; return 0; } Output: src/arays> g++ pointerIndex.cc // compile & run on a Sun station src/arays> a.out px[0] = 23 px[1] = 5 px[-1] = -268437516 src/arays> g++ pointerIndex.cc // compile & run on a Linux box src/arays> ./a.out px[0] = 23 px[1] = -1073743784 px[-1] = -1073743852 src/arays>

Категории