Question : What will be the Output of the Program.


#include<stdio.h>
void main()
{
 char s[ ]="cat";
 int i;
  for(i=0;s[ i ];i++)
    printf("\n%c %c %c %c",s[ i ],*(s+i),*(i+s),i[s]);
}

Answer -

c c c c
a a a a
t t t t

Explanation :

  1. Array name is the base address of the array.
  2. Here ‘s’ is the base address.
  3. ‘i’ is the index number/displacement from the base address.
  4. Array A[i] can be Represented as -
*(Array name + Displacement)
= *(A + i)
= Value at (Array name + Displacement)
= Value at (Base Address + Displacement)

So

s[i] = *(s + i)
i[s] = *(i + s)