Arithmetic Operations on Pointers for GATE Students

Basics Before Solving the Programs :

Basic Tip :
Priority and Associativity between Increment/Decrement (++ or –) Operator and Deferencing Operator(*) :
Priority between ++/-- and * : Same Priority
Associativity                : Right to Left
Consider the following statements in C++ :
1. int a [] = {0,1,2,3,4};
2. int *p[] = {a,a+1,a+2,a+3,a+4}
3. int **ptr = p;
4. (++ptr);
5. (--ptr);
6. *(++ptr);
Meaning of the above Statements :
Statement 1 and 2 :

Arithmetic Operations on Pointers for GATE Students 1

Statement 3, 4 and 5 :

Arithmetic Operations on Pointers for GATE Students 2

Statement 6 :

Arithmetic Operations on Pointers for GATE Students 3

What is the Meaning of (ptr++), (ptr- -) and (ptr – p) ?
ptr++ means → Go to Next Element;
ptr-- means → Go to Previous Element;
ptr-p means → Number of Elements between ptr and p;

Addition and Subtraction on the pointer variable :

Statement Meaning
Addition Between Pointer
Variable and Integer
Constant is Possible :
Let the address of ptr = 2000
ptr + 3 Skipping 3 elements from ptr After
ptr + 5 Skipping 5 elements from ptr After
ptr + i Skipping i elements from ptr After
Subtraction Between Pointer
Variable and Integer
Constant is Possible :
Let the address of ptr = 2010
ptr – 3 Skipping 3 elements from ptr before
ptr – 5 Skipping 5 elements from ptr before
ptr – i Skipping i elements from ptr before
Subtraction Between two
Pointer Variable is also
Possible if they follow the
following Conditions :Conditions to subtract 2 Pointers :

Let there are two pointers named ptr1
and ptr2. The conditions to subtract ptr2 from ptr1 is :

  1. if ptr1 ≥ ptr2
  2. ptr1 and ptr2 must points to
    same array

 

ptr1 – ptr2 Number of Elements between ptr1 and ptr2
Addition, Division and Multiplication Between two pointers is not possible, Because there is no Meaning

Program No 1 :

What will be the output of the following program ? Assume processor is of 16 bit and the starting address of array a and p is 1000 and 2000 respectively. The variable ptr is stored at athe address 3000.

1. main()
2.    {
3.     int a[] = {0,1,2,3,4};
4.     int *p[] = {a,a+1,a+2,a+3,a+4};
5.     int **ptr = p;
6.     printf("%u, %d",a,*a);                
7.     printf("\n%u, %u, %d", p, *p, **p);
8.     printf("\n%u, %u, %d, ptr, *ptr, **ptr);
9.    }
Output of Program No 1 :
1000, 0
2000, 1000, 0
2000, 1000, 0

Program No 2 :

What will be the output of the following program ? Assume processor is of 16 bit and the starting address of array a and p is 1000 and 2000 respectively. The variable ptr is stored at athe address 3000.

   main()
      {
1.     int a[] = {0,1,2,3,4};
2.     int *p[] = {a,a+1,a+2,a+3,a+4};
3.     int **ptr = p;
4.     ptr++;       
5.     printf("%d, %d, %d", ptr-p, *ptr-a, **ptr);
6.     *ptr++;
7.     printf("\n%d, %d, %d", ptr-p, *ptr-a, **ptr);
8.    *(++ptr);
9.    printf("\n%d, %d, %d", ptr-p, *ptr-a, **ptr);
10.    ++(*ptr);
11.    printf("\n%d, %d, %d", ptr-p, *ptr-a, **ptr);
      }
Output of Program No 2 :
1, 1, 1
2, 2, 2
3, 3, 3
3, 4, 4
Explanation of Program No 2 :

Statement 1, 2 and 3 :

Arithmetic Operations on Pointers for GATE Students 4

Statement 4, 5 and 6 :

Arithmetic Operations on Pointers for GATE Students 5 Arithmetic Operations on Pointers for GATE Students 6

Statement 7 :

Arithmetic Operations on Pointers for GATE Students 7

Statement 8 and 9 :

Arithmetic Operations on Pointers for GATE Students 8

Statement 10 :

Arithmetic Operations on Pointers for GATE Students 9

Statement 11 :

Arithmetic Operations on Pointers for GATE Students 10

     

Leave a Reply