Home >>C Tutorial >Arithmetic Pointer in C

Arithmetic Pointer in C

Arithmetic Pointer in C language

The pointer in C language is used to perform arithmetic operations like addition, subtraction, etc. However, as a known fact pointers contains the address and hence, the result of an arithmetic operation executed on the pointer will also come out to be a pointer provided that the other operand is of integer type. The result in a pointer-from-pointer subtraction will be an integer value. Here are the mentioned arithmetic operations that are generally possible on the arithmetic pointers in C language:

  • Decrement
  • Increment
  • Addition
  • Comparison
  • Subtraction

1. Decrementing pointer in C language

Users can decrement a pointer variable in the C language just like increment. The pointer will start pointing to the previous location if the pointer is decremented by the end user. Here is the formula of decrementing the pointer in C language:

new_address= current_address - i * size_of(data type)

The values of decrementing a pointer in the C language depends on the architecture:

32-bit

For a 32-bit int variable, the value will be decremented by 2 bytes.

64-bit

For a 64-bit int variable, the value will be decremented by 4 bytes.

Let's take an example of Decrementing pointer in C:

#include <stdio.h>            
void main(){            
int num=70;        
int *x;//pointer to int      
x=&num ;//stores the address of num variable        
printf("Address of x variable is %u \n",x);        
x=x-1;       
printf("After decrement: Address of x variable is %u \n",x); // x will now point to the immidiate previous location.         
}   
Output :
Address of x variable is 4089061828
After decrement: Address of x variable is 4089061824

2. Incrementing pointer in the C language

The pointer in C will start pointing to the next immediate location, if the pointer is incremented by 1. You will notice that this is different from the general arithmetic as the value of the pointer gets increased by the data type’s size towards which the pointer is pointing.

We can traverse an array by the use of the increment operation on a pointer that will keep pointing towards every element of the array, users can transverse an array and perform various operations on the same and when in loop it can update itself.

Here is the rule to increment the pointer in the C language is as follows:

new_address= current_address + i * size_of(data type

In the above mentioned syntax the 'i' is the value by which the pointer will get increased.

The values of incrementing a pointer in the C language depends on the architecture:

32-bit

For a 32-bit int variable, the value will be incremented by 2 bytes.

64-bit

For a 64-bit int variable, the value will be incremented by 4 bytes.

Let's take an example of Incrementing pointer in C:

#include<stdio.h>  
int main(){  
int num=70;        
int *x;//pointer to int      
x=&num ;//stores the address of num variable        
printf("Address of x variable is %u \n",x);        
x=x+1;        
printf("After increment: Address of x variable is %u \n",x); // in our case, x will get incremented by 4 bytes.      
return 0;  
} 
Output :
Address of x variable is 1239347716
After increment: Address of x variable is 1239347720

3. Addition of the C pointer

A specific value can be added to the pointer variable. Here is the formula for adding the value to a pointer:

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

The values of addition to a pointer in the C language depends on the architecture:

32-bit

For a 32-bit int variable, the value will be added by 2* number.

64-bit

For a 64-bit int variable, the value will be added by 4* number.

Let's take an example of Addition of the C pointer:

#include<stdio.h>  
int main(){  
int num=70;        
int *x;//pointer to int      
x=&num ;//stores the address of num variable        
printf("Address of x variable is %u \n",x);        
x=x+3;   //adding 3 to pointer variable    
printf("After adding 3: Address of x variable is %u \n",x);       
return 0;  
}    
Output :
Address of x variable is 3849020004
After adding 3: Address of x variable is 3849020016

4. Subtraction of pointers in the C language

A value can be subtracted from the pointer just by the same process as in addition in pointers. An address will be the output as the result of subtracting any number from a pointer. Here is the formula of subtracting any value from a pointer variable:

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

The values of substraction to a pointer in the C language depends on the architecture:

32-bit

For a 32-bit int variable, the value will be substracted by 2* number.

64-bit

For a 64-bit int variable, the value will be substracted by 4* number.

Let's take an example of Subtraction of pointers in C:

#include<stdio.h>  
int main(){  
int num=70;        
int *x;//pointer to int      
x=&num ;//stores the address of num variable        
printf("Address of x variable is %u \n",x);        
x=x-3; //subtracting 3 from pointer variable    
printf("After subtracting 3: Address of x variable is %u \n",x);        
return 0;  
}
Output :
Address of x variable is 3917007556
After subtracting 3: Address of x variable is 3917007544

Let's have a look at some of the illegal arithmetic with pointers in the C language

Pointers are very specific when it comes to the operations. Certain operations are there that cannot be performed on the pointers as, pointer are known to store the address hence in order to ignore to get to an illegal address, these operations should not be performed on the pointers. For an instance, addition and multiplication are one of the operations that are on the list. Here is the list of these kind of operations that are known to be illegal in C language:

  • Address ^ Address = illegal
  • ~Address = illegal
  • Address | Address = illegal
  • Address * Address = illegal
  • Address % Address = illegal
  • Address & Address = illegal
  • Address + Address = illegal
  • Address / Address = illegal

Let's understand the pointer to function in C language

As we have already learnt that the pointers are allowed or can point to a function in the C language. But the catch in this is that the declaration of the point variable must be similar to the function. Here is an example that will help you understand the same:

Let's take an example of pointer to function in C :

#include<stdio.h>  
int add ();  
int main ()  
{  
    int result;   
    int (*ptr)();  
    ptr = &add;  
    result = (*ptr)();  
    printf("The sum is %d",result);  
}  
int add()  
{  
    int x, y;   
    printf("Enter two numbers?");  
    scanf("%d %d",&x,&y);  
    return x+y;  
} 
Output :
Enter two numbers?10 15
The sum is 25

Let's understand pointer to array of functions in the C language

It is recommended that you should have the knowledge of the array of the function, in order to understand the array of the functions. Basically the array of the functions is an array that consists of the addresses of the functions. In simple words, the pointer to an array of functions is basically a pointer that is pointing to an array that consists of the pointers to the functions. Here is an example that will elaborate the concept for you:

Let's take an example of pointer to array function in C:

#include<stdio.h>  
int x();  
int xadd(int);  
int (*arr[3])();  
int (*(*ptr)[3])();  
  
int main ()  
{  
    int result1;  
    arr[0] = x;  
    arr[1] = xadd;  
    ptr = &arr;  
    result1 = (**ptr)();  
    printf("printing the value returned by x : %d",result1);  
    (*(*ptr+1))(result1);  
}  
int x()  
{  
    int a = 85;  
    return a++;  
}  
int xadd(int b)  
{  
    printf("\nAdding 105 to the value returned by x: %d",b+105);  
}  
Output :
printing the value returned by x : 85
Adding 90 to the value returned by x: 190

Popular Tutorials