Pointer Arithmetic Operations

We can perform arithmetic operations on pointer variable just as you can a numeric value. As we know that, a pointer in C is a variable which is used to store the memory address which is a numeric value. The arithmetic operations on pointer variable effects the memory address pointed by pointer.

Valid Pointer Arithmetic Operations

  • Adding a number to pointer.
  • Subtracting a number form a pointer.
  • Incrementing a pointer.
  • Decrementing a pointer.
  • Subtracting two pointers.
  • Comparison on two pointers.

Invalid Pointer Arithmetic Operations

  • Addition of two pointers.
  • Division of two pointers.
  • Multiplication of two pointers.

Incrementing a Pointer

Let ptr be an integer pointer which points to the memory location 5000 and size of an integer variable is 32-bit(4 bytes). Now, when we increment pointer ptr

    ptr++;

it will point to memory location 5004 because it will jump to the next integer location which is 4 bytes next to the current location. Incrementing a pointer is not same as incrementing an integer value. On incrementing, a pointer will point to the memory location after skipping N bytes, where N is the size of the data type(in this case it is 4).

ptr++ is equivalent to ptr + (sizeof(pointer_data_type)). 

“Incrementing a pointer increases its value by the number of bytes of its data type”

A character(1 bytes) pointer on increment jumps 1 bytes.
An integer(4 bytes) pointer on increment jumps 4 bytes.

 

This is a very important feature of pointer arithmetic operations which we will use in array traversal using pointers.

Program On Pointer Incrementing Pointer

pointer-arithmetic-program

Output

pointer-arithmetic-output

Decrementing a Pointer

Similarly, Decrementing a pointer will decrease its value by the number of bytes of its data type. Hence, after

    ptr--;

ptr will point to 4996.

ptr--; is equivalent to ptr - (sizeof(pointer_data_type)).

Program On Decrementing Pointer

decrementing-program-2

Output

decrementing-output-2

Pointer Comparision

Pointers may be compared by using relational operators, such as ==, <, and >. If p1 and p2 point to variables that are related to each other, such as elements of the same array, then p1 and p2 can be meaningfully compared.

The following program modifies the previous example − one by incrementing the variable pointer so long as the address to which it points is either less than or equal to the address of the last element of the array, which is &var[MAX – 1] −

Program On Pointer Comparison

pointer-comparision1

Output

pointer-comparisionoutput

Pointer Addition

In C Programming we can add any integer number to Pointer variable. It is perfectly legal in c programming to add integer to pointer variable.

Adding a number N to a pointer leads the pointer to a new location after skipping N times size of data type.

ptr + N =  ptr + (N * sizeof(pointer_data_ype))

For example, Let ptr be a 4-byte integer pointer, initially pointing to location 5000.
Then ptr + 5 = 5000 + 4*5 = 5020. Pointer ptr will now point at memory address 5020.

Program On Pointer Addition

adding-pointer-program

Output

adding-pointer-output

Pointer Subtarction

Subtracting a number N from a pointers is similar to adding a number except in Subtraction the new location will be before current location by N times size of data type.

ptr - N =  ptr - (N * sizeof(pointer_data_ype))

For example, Let ptr be a 6-byte double pointer, initially pointing to location 5000.
Then ptr – 3 = 5000 – 6*3 = 4982. Pointer ptr will now point at memory address 4982.

Program On Pointer Subtraction

subtracting-pointer

Output

subtracting-pointer-output