C Pointer Arithmetic Operations And Examples

  • Decrement
  • Addition
  • Subtraction
  • Comparison
  • Incrementing Pointer in C

If we increase a pointer by 1, the pointer will begin pointing to the next consecutive memory location. This behavior differs from standard arithmetic, as the pointer's value increments by the size of the data type it references.

We have the ability to navigate through an array by employing the increment functionality on a pointer that will continuously reference each element within the array, execute specific actions on it, and adjust its position iteratively within a loop.

The Rule to increment the pointer is given below:

Example

new_address= current_address + i * size_of(data type)

Where 'i' represents the value by which the pointer is incremented.

32-bit

For a 32-bit integer variable, it will be increased by 2 bytes.

64-bit

For a variable of 64-bit integer type, it will increase by 4 bytes when incremented.

Let's explore an example of incrementing a pointer variable in a 64-bit architecture.

Example

Example

#include<stdio.h>

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

Output

Address of p variable is 3214864300 

After increment: Address of p variable is 3214864304

Traversing an array by using pointer

Example

Example

#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

Output

printing array elements...

1  2  3  4  5

Decrementing Pointer in C

Similarly to incrementing, it is possible to decrease a pointer variable. When a pointer is decremented, it shifts to the preceding memory location. The process of decrementing a pointer follows the formula below:

Example

new_address= current_address - i * size_of(data type)

32-bit

For a 32-bit integer variable, it will be reduced by 2 bytes.

64-bit

For a 64-bit integer variable, it will be reduced by 4 bytes.

Let's observe an illustration of decreasing a pointer variable on a 64-bit operating system.

Example

Example

#include <stdio.h>          

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

Output

Address of p variable is 3214864300 

After decrement: Address of p variable is 3214864296

C Pointer Addition

We have the ability to assign a value to the pointer variable. The process of adding a value to a pointer is outlined as follows:

Example

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 explore an illustration of incrementing a pointer variable on a 64-bit system.

Example

Example

#include<stdio.h>

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

Output

Address of p variable is 3214864300 

After adding 3: Address of p variable is 3214864312

Upon examination, the memory address assigned to variable p is 3214864300. However, upon adding 3 to the p variable, the address now becomes 3214864312, indicating an increment of 12. This increment aligns with the 64-bit architecture standard, as each integer value typically occupies 8 bytes of memory.

In contrast, under a 32-bit architecture, the increment would be 6 instead of 12. This is because in a 32-bit OS, integer values typically occupy 4 bytes of memory, resulting in a different increment calculation where 2 * 3 equals 6.

C Pointer Subtraction

Similar to incrementing a pointer, it is also possible to decrement a pointer by a specified value. When subtracting a value from a pointer, the result will be a memory address. The process of subtracting a value from a pointer variable can be calculated using the following formula:

Example

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

32-bit

For a 32-bit integer variable, it will deduct twice the value of the number.

64-bit

For a 64-bit integer variable, it will deduct 4 times the value of the number.

Let's explore an illustration of decrementing a value from the pointer variable in a 64-bit system.

Example

Example

#include<stdio.h>

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

Output

Address of p variable is 3214864300 

After subtracting 3: Address of p variable is 3214864288

You can observe that the pointer variable is now 12 units lower than the previous memory address after subtracting 3 from it, which is equivalent to 4 multiplied by 3.

However, instead of performing subtraction with numerical values, it is also possible to subtract one memory address from another (pointers). This operation will yield a numerical value. It is not a straightforward arithmetic calculation, but it abides by the following principle.

If two pointers are of the same type,

Example

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

Consider this illustration for subtracting one pointer from another.

Example

Example

#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

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
  • Pointer to function in C

As previously mentioned in the preceding section, a pointer in C has the capability to reference a function. Nevertheless, it is imperative that the pointer variable's declaration matches that of the function. Below is an illustration demonstrating how to create a pointer that references a function.

Example

Example

#include<stdio.h>

int addition ();

int main ()

{

	int result; 

	int (*ptr)();

	ptr = &addition;

	result = (*ptr)();

	printf("The sum is %d",result);

}

int addition()

{

	int a, b; 

	printf("Enter two numbers?");

	scanf("%d %d",&a,&b);

	return a+b;

}

Output

Output

Enter two numbers?10 15 

The sum is 25

Pointer to Array of functions in C

To grasp the idea behind an array of functions, it is essential to comprehend what it entails. Essentially, an array of functions is a data structure that stores the memory addresses of functions. In simpler terms, a pointer to an array of functions refers to a pointer that directs to an array storing pointers to various functions. Let's illustrate this with the following example.

Example

Example

#include<stdio.h>

int show();

int showadd(int);

int (*arr[3])();

int (*(*ptr)[3])();



int main ()

{

    int result1;

	arr[0] = show;

	arr[1] = showadd;

	ptr = &arr;

	result1 = (**ptr)();

	printf("printing the value returned by show : %d",result1);

	(*(*ptr+1))(result1);

}

int show()

{

	int a = 65;

	return a++;

}

int showadd(int b)

{

    printf("\nAdding 90 to the value returned by show: %d",b+90);

}

Output

Output

printing the value returned by show : 65 

Adding 90 to the value returned by show: 155

Input Required

This code uses input(). Please provide values below: