Share via


Addition of Pointer Types

If one of the operands in an addition operation is a pointer to an array of objects, the other must be of integral type. The result is a pointer that is of the same type as the original pointer and that points to another array element. The following code fragment illustrates this concept:

short IntArray[10]; // Objects of type short occupy 2 bytes
short *pIntArray = IntArray;

for( int i = 0; i < 10; ++i )
{
    *pIntArray = i;
    cout << *pIntArray << "\n";
    pIntArray = pIntArray + 1;
}

Although the integral value 1 is added to pIntArray, it does not mean "add 1 to the address"; rather it means "adjust the pointer to point to the next object in the array" that happens to be 2 bytes (or sizeof( int )) away.

Note

Code of the form pIntArray = pIntArray + 1 is rarely found in C++ programs; to perform an increment, these forms are preferable: pIntArray++ or pIntArray += 1.

See Also

Reference

Expressions with Binary Operators