Pointer Arithmetics in C

A pointer variable in C stores the address of another variable. The address is always an integer. So, can we perform arithmetic operations such as addition and subtraction on the pointers? In this chapter, we will explain which arithmetic operators use pointers in C as operands, and which operations are not defined to be performed with pointers.

Increment/Decrement of a Pointer

We know that “++” and “–” are used as the increment and decrement operators in C. They are unary operators, used in prefix or postfix manner with numeric variable operands, and they increment or decrement the value of the variable by one.

Assume that an integer variable “x” is created at address 1000 in the memory, with 10 as its value. Then, “x++” makes the value of “x” as 11.

int x =10;// created at address 1000

x++;// x becomes 11

What happens if we declare “y” as pointer to “x” and increment “y” by 1 (with “y++”)? Assume that the address of “y” itself is 2000.

int x =10;// created at address 1000// "y" is created at address 2000 // it holds 1000 (address of "x")int*y =&x ;

y++;// y becomes 1004

Since the variable “y” stores 1000 (the address of “x”), we expect it to become 1001 because of the “++” operator, but it increments by 4, which is the size of “int” variable.

The is why because, if the address of “x” is 1000, then it occupies 4 bytes: 1000, 1001, 1002 and 1003. Hence, the next integer can be put only in 1004 and not before it. Hence “y” (the pointer to “x”) becomes 1004 when incremented.

Example 1

The following example shows how you can increment a pointer −

#include <stdio.h>intmain(){int x =10;int*y =&x;printf("Value of y before increment: %d\n", y);

   y++;printf("Value of y after increment: %d", y);}

Output

Run the code and check its output −

Value of y before increment: 6422036
Value of y after increment: 6422040

You can see that the value has increased by 4. Similarly, the “–” operator decrements the value by the size of the data type.

Example 2

Let us change the types of “x” and “y” to “double” and “float” and see the effect of decrement operator.

#include <stdio.h>intmain(){double x =10;double*y =&x;printf("value of y before decrement: %ld\n", y);
   
   y--;printf("value of y after decrement: %ld", y);}

Output

Value of y before decrement: 6422032
Value of y after decrement: 6422024

When an array is declared, the elements are stored in adjacent memory locations. In case of “int” array, each array subscript is placed apart by 4 bytes, as the following figure shows −

Memory Locations

Hence, if a variable stores the address of 0th element of the array, then the “increment” takes it to the 1st element.

Example 3

The following example shows how you can traverse an array by incrementing a pointer successively −

#include <stdio.h>intmain(){int a[]={10,20,30,40,50,60,70,80,90,100};int len =sizeof(a)/sizeof(int);int*x = a;int i =0;for(i =0; i < len; i++){printf("Address of subscript %d = %d Value = %d\n", i, x,*x);
      x++;}return0;}

Output

Run the code and check its output −

Address of subscript 0 = 6421984 Value = 10
Address of subscript 1 = 6421988 Value = 20
Address of subscript 2 = 6421992 Value = 30
Address of subscript 3 = 6421996 Value = 40
Address of subscript 4 = 6422000 Value = 50
Address of subscript 5 = 6422004 Value = 60
Address of subscript 6 = 6422008 Value = 70
Address of subscript 7 = 6422012 Value = 80
Address of subscript 8 = 6422016 Value = 90
Address of subscript 9 = 6422020 Value = 100

Addition/Subtraction of Pointers

We are familiar with the “+” and “−” operators when they are used with regular numeric operands. However, when you use these operators with pointers, they behave in a little different way.

Since pointers are fairly large integers (especially in modern 64-bit systems), addition of two pointers is meaningless. When we add a 1 to a pointer, it points to the next location where an integer may be stored. Obviously, when we add a pointer (itself a large integer), the location it points may not be in the memory layout.

However, subtraction of two pointers is realistic. It returns the number of data types that can fit in the two pointers.

Example

Let us take the array in the previous example and perform the subtraction of pointers of a[0] and a[9]

#include <stdio.h>intmain(){int a[]={10,20,30,40,50,60,70,80,90,100};int*x =&a[0];// zeroth elementint*y =&a[9];// last elementprintf("Add of a[0]: %ld add of a[9]: %ld\n", x, y);printf("Subtraction of two pointers: %ld", y-x);}

Output

Run the code and check its output −

Add of a[0]: 140729162482768 add of a[9]: 140729162482804
Subtraction of two pointers: 9

It can be seen that the numerical difference between the two integers is 36; it suggests that the subtraction is 9, because it can accommodate 9 integers between the two pointers.

Pointer Comparisons

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.

Example

The following program modifies the previous example. We will keep incrementing the variable pointer as 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]”.

#include <stdio.h>constint MAX =3;intmain(){int var[]={10,100,200};int i,*ptr;/* address of the first element in pointer */
   ptr = var;
   i =0;while(ptr <=&var[MAX -1]){printf("Address of var[%d] = %d\n", i, ptr );printf("Value of var[%d] = %d\n", i,*ptr );/* point to the previous location */
      ptr++;
      i++;}return0;}

Output

Run the code and check its output −

Address of var[0] = 6422020
Value of var[0] = 10

Address of var[1] = 6422024
Value of var[1] = 100

Address of var[2] = 6422028
Value of var[2] = 200

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *