Pointer Arithmetic in C

Pointer Arithmetic in C
Techiio-author
Written by Shuvhojit DebFebruary 6, 2022
14 min read
C
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Techiio-author
Shuvhojit Deb

Full Stack Developer

In this article, we'll know about the pointer arithmetic of C.

Pointer

A pointer in c is an address, which is a numeric value. Therefore, you can perform arithmetic operations on a pointer just as you can on a numeric value. There are four arithmetic operators that can be used on pointers: ++, --, +, and -

To understand pointer arithmetic, let us consider that ptr is an integer pointer which points to the address 1000. Assuming 32-bit integers, let us perform the following arithmetic operation on the pointer −

ptr++

After the above operation, the ptr will point to the location 1004 because each time ptr is incremented, it will point to the next integer location which is 4 bytes next to the current location. This operation will move the pointer to the next memory location without impacting the actual value at the memory location. If ptr points to a character whose address is 1000, then the above operation will point to location 1001 because the next character will be available at 1001.

Incrementing

We prefer using a pointer in our program instead of an array because the variable pointer can be incremented, unlike the array name which cannot be incremented because it is a constant pointer. The following program increments the variable pointer to access each succeeding element of the array −

#include <stdio.h>
const int MAX = 3;
int main () {
   int  var[] = {10, 100, 200};
   int  i, *ptr;
   /* let us have array address in pointer */
   ptr = var;
	
   for ( i = 0; i < MAX; i++) {
      printf("Address of var[%d] = %x\n", i, ptr );
      printf("Value of var[%d] = %d\n", i, *ptr );
      /* move to the next location */
      ptr++;
   }
	
   return 0;
}

Output:

Address of var[0] = bf882b30
Value of var[0] = 10
Address of var[1] = bf882b34
Value of var[1] = 100
Address of var[2] = bf882b38
Value of var[2] = 200

Decrementing

The same considerations apply to decrementing a pointer, which decreases its value by the number of bytes of its data type as shown below −

#include <stdio.h>
const int MAX = 3;
int main () {
   int  var[] = {10, 100, 200};
   int  i, *ptr;
   /* let us have array address in pointer */
   ptr = &var[MAX-1];
	
   for ( i = MAX; i > 0; i--) {
      printf("Address of var[%d] = %x\n", i-1, ptr );
      printf("Value of var[%d] = %d\n", i-1, *ptr );
      /* move to the previous location */
      ptr--;
   }
	
   return 0;
}

Output:

Address of var[2] = bfedbcd8
Value of var[2] = 200
Address of var[1] = bfedbcd4
Value of var[1] = 100
Address of var[0] = bfedbcd0
Value of var[0] = 10

Addition

We cannot add the two pointers, as it would also result in the address of an unknown memory location. So there is no use of it. But we can add any integer value to the pointer in order to point to that memory location. Addition of integer value works according to the datatype of the value pointer pointing to using ptr+x. For example, if the pointer holds the address of any integer value (64 – bit integer system having 4 bytes integer), on adding +2 in it, it will increment the value by 8 bytes.

#include<stdio.h>
int main(){
double num = 50.3;
char a = 'u';
// pointer 'ptr' to point the above 'num' and 'ptr1' to point 'a'
double *ptr;
char *ptr1;
// pointer 'ptr' holding the address of 'num' location and 'ptr1' to hold the address of character 'a'
ptr = &num;
ptr1 = &a;
printf("\n The address which the pointer holds is %u",ptr);
printf("\n The address which the pointer holds is %u",ptr1);
// adding the integer value 4 to the pointer value
ptr = ptr + 4;
ptr1 = ptr1 + 4;
// Pointer address will now gets incremented by 4*8 bytes as it holds the address of double value
printf("\n Now the address which the pointer holds is %u",ptr);
// Pointer address will now gets incremented by 4*1 bytes as it holds the address of character value
printf("\n Now the address which the pointer holds is %u",ptr1);
return 0;
}

Output:

The address which the pointer holds is 322376700
The address which the pointer holds is 3223766999
Now the address which the pointer holds is 3223767032
Now the address which the pointer holds is 3223767003

Subtraction

Subtraction in case of pointers is possible with two addresses (i.e. with 2 pointer values) as well as subtraction of an integer value from the pointer. Subtraction of an integer value from the pointer works similar to the addition of integer value as discussed above, i.e. any integer value can be subtracted from the pointer using ptr-x. And it will result the difference of pointer by x * bytes of value datatype hold by pointer.

In subtraction of 2 pointers, both the pointers need to be of the same data type and it results in an integer value which is useful in case of arrays when we want to find the number of elements in between them using the 2 addresses.

Simple syntax to find the number of elements in between the 2 addresses of pointers is: (ptr2 – ptr1)/size of datatype that pointer holds.

#include<stdio.h>
int main(){
double num = 50.3;
char a = 'u';
// pointer 'ptr' to point the above 'num' and 'ptr1' to point 'a'
double *ptr;
char *ptr1;
// pointer 'ptr' holding the address of 'num' location and 'ptr1' to hold the address of character 'a'
ptr = &num;
ptr1 = &a;
printf("\n The address which the pointer holds is %u",ptr);
printf("\n The address which the pointer holds is %u",ptr1);
// subtracting the integer value 4 to the pointer value
ptr = ptr - 4;
ptr1 = ptr1 - 4;
// Pointer address will now gets decreased by 4*8 bytes as it holds the address of double value
printf("\n Now the address which the pointer holds is %u",ptr);
// Pointer address will now gets decreased by 4*1 bytes as it holds the address of character value
printf("\n Now the address which the pointer holds is %u",ptr1);
return 0;
}

Output:

The address which the pointer holds is 2459298328
The address which the pointer holds is 2459298327
Now the address which the pointer holds is 2459298296
Now the address which the pointer holds is 2459298323

Comparison

C provides a wonderful feature of comparing the 2 pointers with only the condition being that both the pointers are of the same type and pointing to the same array. All the comparison operations like (>, <, <=, >=, ==, !=) can be performed on them. Infact, C does not throw an error on the console in comparison of 2 pointers pointing to different datatype.

#include <stdio.h>
int main()
{
int arr1[6] = {100, 200, 300, 400, 500, 600};
// pointer 'ptr1' pointing to the address of 1st array element
int *ptr1 = &arr1[0];
printf("\n Array elements are given below:");
while(ptr1 < &arr1[6])
{
printf("\n array value is %d ", *ptr1);
//Incrementing the pointer to move to the address of next element
ptr1++;
}
return 0;
}

Output:

Array elements are given below:
array value is 100
array value is 200
array value is 300
array value is 400
array value is 500
array value is 600

Conclusion

Above description easily explains what are pointers and the colorful computation operations that can be performed on them inC. Pointers are veritably useful when performing operations in arrays or in other data structures like linked list, mound, etc. One needs to understand them completely before enforcing them in the law as occasionally they return veritably absurd results. Like addition of 2 pointers is possible but there's no use of it as it would affect in the address of some memory position which we do n’t know.

C language
Pointer Arithmetic
C Pointer
Programming
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance