Home Segments Index Top Previous Next

343: Mainline

The incrementing and decrementing operators, ++ and --, also behave in a special way when used with pointers. They do not add 1; instead, they increment the pointer by the number of bytes occupied by the objects identified by the pointer. In a typical implementation, incrementing a character pointer would increment the address by 1, because a character occupies 1 byte. Incrementing an integer pointer, however, would increment the address by 4, assuming an integer occupies 4 bytes in the implementation. Incrementing a trade pointer would increment the address by 12, assuming trade objects consist of an 8-byte floating-point number and a 4-byte integer.

Suppose, once again, that you assign the address of the first element in an integer array to a pointer variable:

nptr = &number[0]; 

Then, you can reassign the pointer such that it points to the addresses of other array elements:

nptr         /* Pointer identifies the address of the first element */ 
++nptr       /* Pointer identifies the address of the second element */ 
++nptr       /* Pointer identifies the address of the third element */