C Pointer Arithmetic

A pointer in C is an address, which is a numeric value. So you can perform arithmetic operations like addition, subtraction, etc. on a pointer just as you can on a numeric value. As pointer contains the address, the result of an arithmetic operation performed on the pointer will also be a pointer if the other operand is of type integer.

Below arithmetic operators that can be used on pointers.

  • Increment (++)
  • Decrement (--)
  • Addition (+)
  • Subtraction (-)

Incrementing Pointer in C

If we increment a pointer by 1, the pointer will start pointing to the immediate next location. But note that the value of the pointer will get increased by the size of the data type to which the pointer is pointing.

Formula
new_address= current_address + i * size_of(data type)

Where i is the number by which the pointer get increased.

32-bit

For 32-bit int variable, it will be incremented by 2 bytes.

64-bit

For 64-bit int variable, it will be incremented by 4 bytes.

Let's see the example of incrementing pointer variable on 64-bit architecture.

snippet
#include
int main(){
int number=50;      
int *p;//pointer to int    
p=&number;//stores the address of number variable      
printf("Address of p variable is %u \n",p);      
p=p+1;      
printf("After increment: Address of p variable is %u \n",p); // in our case, p will get incremented by 4 bytes.    
return 0;
}
Output
Address of p variable is 3214864300 After increment: Address of p variable is 3214864304

Traversing an array by using pointer

snippet
#include<stdio.h>
void main ()
{
	int arr[5] = {1, 2, 3, 4, 5};
	int *p = arr;
	int i;
	printf("printing array elements...\n");
	for(i = 0; i< 5; i++)
	{
		printf("%d  ",*(p+i));
	}
}
Output
printing array elements... 1 2 3 4 5

Decrementing Pointer in C

Like increment, we can decrement a pointer variable which decreases its value by the number of bytes of its data type. If we decrement a pointer, it will start pointing to the previous location.

Formula
new_address= current_address - i * size_of(data type)

32-bit

For 32-bit int variable, it will be decremented by 2 bytes.

64-bit

For 64-bit int variable, it will be decremented by 4 bytes.

Let's see the example of decrementing pointer variable on 64-bit OS.

snippet
#include           
void main(){          
int number=50;      
int *p;//pointer to int    
p=&number;//stores the address of number variable      
printf("Address of p variable is %u \n",p);      
p=p-1;     
printf("After decrement: Address of p variable is %u \n",p); // P will now point to the immidiate previous location.       
}
Output
Address of p variable is 3214864300 After decrement: Address of p variable is 3214864296

C Pointer Addition

We can add a value to the pointer variable.

Formula
new_address= current_address + (number * size_of(data type))

32-bit

For 32-bit int variable, it will add 2 * number.

64-bit

For 64-bit int variable, it will add 4 * number.

Let's see the example of adding value to pointer variable on 64-bit architecture.

snippet
#include
int main(){
int number=50;      
int *p;//pointer to int    
p=&number;//stores the address of number variable      
printf("Address of p variable is %u \n",p);      
p=p+3;   //adding 3 to pointer variable  
printf("After adding 3: Address of p variable is %u \n",p);     
return 0;
}
Output
Address of p variable is 3214864300 After adding 3: Address of p variable is 3214864312

As you can see, the address of p is 3214864300. But after adding 3 with p variable, it is 3214864312, i.e., 4*3=12 increment. Since we are using 64-bit architecture, it increments 12. But if we were using 32-bit architecture, it was incrementing to 6 only, i.e., 2*3=6. As integer value occupies 2-byte memory in 32-bit OS.

C Pointer Subtraction

Like pointer addition, we can subtract a value from the pointer variable. Subtracting any number from a pointer will give an address. The formula of subtracting value from the pointer variable is given below:

Formula
new_address= current_address - (number * size_of(data type))

32-bit

For 32-bit int variable, it will subtract 2 * number.

64-bit

For 64-bit int variable, it will subtract 4 * number.

Let's see the example of subtracting value from the pointer variable on 64-bit architecture.

snippet
#include
int main(){
int number=50;      
int *p;//pointer to int    
p=&number;//stores the address of number variable      
printf("Address of p variable is %u \n",p);      
p=p-3; //subtracting 3 from pointer variable  
printf("After subtracting 3: Address of p variable is %u \n",p);      
return 0;
}
Output
Address of p variable is 3214864300 After subtracting 3: Address of p variable is 3214864288

You can see after subtracting 3 from the pointer variable, it is 12 (4*3) less than the previous address value.

However, instead of subtracting a number, we can also subtract an address from another address (pointer). This will result in a number. It will not be a simple arithmetic operation, but it will follow the following rule.

If two pointers are of the same type,

snippet
Address2 - Address1 = (Subtraction of two addresses)/size of data type which pointer points

Consider the following example to subtract one pointer from an another.

snippet
#include<stdio.h>
void main ()
{
	int i = 100; 
	int *p = &i;
	int *temp;
	temp = p; 
	p = p + 3;
	printf("Pointer Subtraction: %d - %d = %d",p, temp, p-temp);
}
Output
Pointer Subtraction: 1030585080 - 1030585068 = 3

Illegal arithmetic with pointers

There are various operations which can not be performed on pointers. Since, pointer stores address hence we must ignore the operations which may lead to an illegal address, for example, addition, and multiplication. A list of such operations is given below.

  • Address + Address = illegal
  • Address * Address = illegal
  • Address % Address = illegal
  • Address / Address = illegal
  • Address & Address = illegal
  • Address ^ Address = illegal
  • Address | Address = illegal
  • ~Address = illegal
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +