Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +917541905230, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Sunday, December 6, 2020

Pointer's Arithmetic in C

Pointer's Arithmetic | welcome2protec.com

We can add and subtract an integer to/from an address. However, We can get address of next element by incrementing pointers with ++ (i.e. ptr++). as we know that pointer contains the address, so the result of an arithmetic operation performed on the pointer will be a pointer. Have a look at the below example how to find the address of next elements by performing arithmetic operation on pointers.


Accessing array elements by using subscript:

Before going further in this tutorial let's remind how to access array elements using subscript:

/*Accessing array elements using subscript*/

arr[0] = 2   // To access 1st elements
arr[1] = 4   // To access 2nd element
arr[2] = 6   // To access 3rd element
arr[3] = 8   // To access 4th element
arr[4] = 10  // To access 5th element
arr[5] = 12  // To access 6th element

Pointer's Arithmetic | welcome2protec.com
Pointer's Arithmetic | welcome2protec.com

Accessing array elements by using Pointer:

Same way if you want to access these elements using pointers so, here pointers arithmetic comes into the picture. Since we know that pointer contains the base address (i.e. address of 1st element equivalent to ptr = &arr[0]). Therefore, We can get address of next element by incrementing pointers with ++ (i.e. ptr++) Like this:

/*Accessing array elements using pointers*/
  
  Using pointer 	 Using Subscript 
  *(ptr + 0) = 2      ==   arr[0]   //to access the 1st element
  *(ptr + 1) = 4      ==   arr[1]   //to access the 2nd element
  *(ptr + 2) = 6      ==   arr[2]   //to access the 3rd element
  *(ptr + 3) = 8      ==   arr[3]   //to access the 4th element
  *(ptr + 4) = 10     ==   arr[4]   //to access the 5th element
  *(ptr + 5) = 12     ==   arr[5]   //to access the 6th element

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++ // i.e. equivalent to ptr = ptr + 1;

After the above operation, the ptr will point to the next location 1004 because each time ptr is incremented, it will point to the next  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 would points to a character whose address is 1000, then the above operation will point to the location 1001 because the next character will be available at 1001.


How to get address of nth block using pointer?

We can add and subtract an integer to/from an address. However, We can get address of next element by incrementing pointers with ++ (i.e. ptr++). as we know that pointer contains the address, so the result of an arithmetic operation performed on the pointer will be a pointer. Have a look at the below example how to find the address of next elements by performing arithmetic operation on pointers.

/* You can also use formula to find the address of nth block 
 * by adding and subtraction an integer to/from an address.*/
 
Formula: 
  Pointer + n = value of pointer + sizeof (pointer’s type) * n 

For Example:
  ptr + 0 = 1000 + 4 * 0 = 1000  //address of 1st element i.e. &arr[0]
  ptr + 1 = 1000 + 4 * 1 = 1004  //address of 2nd element i.e. &arr[1]
  ptr + 2 = 1000 + 4 * 2 = 1008  //address of 3rd element i.e. &arr[2]
  ptr + 3 = 1000 + 4 * 3 = 1012  //address of 4th element i.e. &arr[3]
  ptr + 4 = 1000 + 4 * 4 = 1016  //address of 5th element i.e. &arr[4]

Example 1: Incrementing a pointer:

There are four arithmetic operators that can be used on pointers: ++, --, +, and -. Incrementing pointers with ++, and decrementing with -- is useful when iterating over each elements in an array of elements. We can access the memory location of next element by incrementing pointers with ++ (i.e. ptr++). Have a look at below example.

#include <stdio.h> 
int main
{
 int arr[] = {2, 4, 6, 8, 10, 12};
 int i, *ptr;
 
 /* Assigning base address to a pointer (OR Initializing of pointer) 
  * array name 'arr' represent the base address i.e. arr == &arr[0] */   
 ptr = arr; 
 
 /*Printing array elements and address of each elements using pointers*/
 for(i=0; i<6 i++){
  printf("arr[%d] = %d and address of arr[%d] = %p", i, *(ptr+i), i, ptr+i);
 }
 return 0;
}

Output

arr[0] = 2  and address of arr[0] = 0x9ff02
arr[1] = 4  and address of arr[1] = 0x9ff04
arr[2] = 6  and address of arr[2] = 0x9ff06
arr[3] = 8  and address of arr[3] = 0x9ff08
arr[4] = 10 and address of arr[4] = 0x9ff0A
arr[5] = 12 and address of arr[5] = 0x9ff0B

Example 2: Decrementing a pointer

You can print array elements in reverse order by decrementing a pointer like this:

#include <stdio.h> 
 
const int size = 6; 
 
int main(){ 
   int  arr[size] = {2, 4, 6, 8, 10, 12}; 
   int  i, *ptr; 
 
   /* Initializing of pointer */ 
   ptr = &arr[size-1]; 
	 
   for (i = size; i > 0; i--) { 
      printf("arr[%d] = %d and address of arr[%d] = %p", i-1, *ptr, i-1, ptr);
 
      /* To access previous location */ 
      ptr--; 
   }  
   return 0; 
}

Output

arr[5] = 12  and address of arr[5] = 0x9ff0B
arr[4] = 10  and address of arr[4] = 0x9ff0A
arr[3] = 8   and address of arr[3] = 0x9ff08
arr[2] = 6   and address of arr[2] = 0x9ff06
arr[1] = 4   and address of arr[1] = 0x9ff04
arr[0] = 2   and address of arr[0] = 0x9ff02

Example 3: Pointers Comparision:

Pointers may be compared by using relational operators, such as ==, <, and >.

#include <stdio.h>  
#define int SIZE = 6; 
 
int main(){ 
   int  arr[] = {2, 4, 6, 8, 10, 12}; 
   int  i = 0, *ptr; 
 
   /* let us have address of the first element in pointer */ 
   ptr = arr; 
	 
   while ( ptr <= &arr[SIZE - 1] ){
    printf("arr[%d] = %d and address of arr[%d] = %p", i, *(ptr+i), i, ptr+i);
    i++; 
   }  
   return 0; 
} 

Output

arr[0] = 2  and address of arr[0] = 0x9ff02
arr[1] = 4  and address of arr[1] = 0x9ff04
arr[2] = 6  and address of arr[2] = 0x9ff06
arr[3] = 8  and address of arr[3] = 0x9ff08
arr[4] = 10 and address of arr[4] = 0x9ff0A
arr[5] = 12 and address of arr[5] = 0x9ff0B






Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Help others by sharing this page.

Ahmad Irshad

Author & Editor

I love blogging, teaching, learning computer science and sharing it to others. I've written and develped this site so that students may learn computer science related tutorials eaisly. MCA / MCITP

0 Comments:

Post a Comment

Please don't enter any spam link in the comment box.


Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec
Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec
Contact Us