Pointer arithmetic and address arithmetic

Pointer arithmetic refers to set of operations which can be done on pointer variable:

  • Assignment of pointer of same type
  • Adding or subtracting pointer to int
  • Subtracting or comparing two pointers to members of same array
  • Assigning or comparing pointer to 0 or NULL

Assignment of pointer of same type:

Example 1:

#include <stdio.h>

int main()
{
  int var =10;
  int *ptr = &var;
  int *ptr1 = ptr;
  printf("\n Address are %x %x\n", ptr, ptr1);
  return 0;
}

Output:

Address are e65ce3bc e65ce3bc

Example 2:

#include <stdio.h>

int main()
{
  int arr[5];
  int *ptr = arr;
  int *ptr1 = &arr[0];
  printf("\n Address are %x %x\n", arr, ptr, ptr1);
  return 0;
}

Output:

Address are cb0fff00 cb0fff00

Assigning or comparing pointer to 0 or NULL:

A pointer can be initialized to 0 or NULL. Comparing against 0 or NULL pointer is a way to check whether the pointer holds the valid address or not.

If it is not pointing to any location, it is termed as NULL pointer.

#include <stdio.h>
#include <stdlib.h>

int main()
{
  char *ptr = NULL;
  ptr = malloc(sizeof (char));
  if (ptr == NULL)
    printf("\n It is a NULL pointer");
  else
    printf("\n Address of pointer is %x\n", ptr);
  return 0;
 }
  

Output:

Address of pointer is b1c010

Adding or subtracting pointer to int:

#include <stdio.h>

int main()
{
  int a =10;
  int *ptr1 = &a;
  int *ptr2 = &a;
  pt2 = ptr2 + 4;
  int diff = ptr2 - ptr1;
  printf("\n Address are %x %x\n", ptr1, ptr2);
  printf("\n The value is %d\n", diff);
  return 0;
 }

Output:

Address are 0x7ffcbb8cd628 0x7ffcbb8cd638

The value is 4

Size of integer is 4 bytes.

Pointer arithmetic on arrays:

An array is a variable which contains multiple values of same type. Each element in an array are sequentially placed in memory.

Array and pointers are tightly coupled with each other. Array can be accessed even with pointers.

Syntax of Array:

data_type array_name[size_of_array];

Example:

int array[10];

Array name acts like a pointer constant that is its address cannot be changed and it stores the address of first element in an array.

Hence array or &array[0] can be used to reference array as a pointer.

Array representation in memory:

int array[3] = { 5,10 ,15};

where size of each element is 4 bytes.

untitled image

Code Snippet

#include <stdio.h>

int main()
{

  /* Below two instructions are same, with and without the size*/

  int arr[3]= { 10, 15,20};
  int arr1[]= { 10, 15,20};

  /* Below two instruction are equivalent */

  int *ptr = arr;
  int *ptr1 = &arr[0];

  printf(“\n Address of original ptr1 is %p and value is %d\n”, ptr1, *ptr1);

  /* Adding integer to array */

  ptr1= arr + 2;

  printf(“\n Address of pointer after addition is %p and value is %d”, ptr1, *ptr1);

  /* Subtracting integer to array*/

  ptr1 = ptr1 -2;

  printf(“\n Address of pointer after subtraction is %p and value is %d\n”, ptr1, *ptr1);

/* Accessing the 1-D array with pointers */

  for (i; i <sizeof(arr)/sizeof(int); i++)

     printf(” %d “, *(arr +i));  // equivalent printf(” %d”, arr[i]);

  return 0;
}

Output:

Address of original ptr1 is 0x7ffc63e1a7a0 and value is 10

Address of pointer after addition is 0x7ffc63e1a7a8 and value is 20

Address of pointer after subtraction is 0x7ffc63e1a7a0 and value is 10

10 15 20

Accessing 2-D array with pointers

#include <stdio.h>
#define no_of_col 3
#define no_of_row 3
int main()
{
  int arr[3][3]= { {1,2,3}, {4,5,6}, {7,8,9}};
  int *ptr = &arr[0][0];
  int i=0, j=0;
  for (i; i <no_of_row; i++)
  {
     int j = 0;
     for (j; j<no_of_col; j++)
       printf(" %d ",*(ptr + (i *no_of_col + j)));
   }
   return 0;
  }

Output:
  1  2  3  4  5  6  7  8  9

For 1-D array

Address of ith element = base address + i * size of data type

For 2-D array

Address of arr[i][j] = base address + ((i *no_of_col +j)) * size of data_type)



Categories: C Language

Leave a comment