Character Array MCQ 11 : Assigning Value to String Variable Using Pointer




Character Array MCQ 11 : Assigning Value to String Variable Using Pointer


What is the Output of the Following Program ?

#include<stdio.h>
#include<string.h>
void main()
{
char str[10];
int i;
for(i=0;i<5;i++)
  i[str] = 'A';
i[str]='\0';
printf("%s",str);
}

Options :

  1. AAAAA
  2. AAAAA Garbage
  3. Garbage
  4. Syntax Error

Output:


Switch to String MCQ Home : Click Here


How and Why ?
Following 4 Statements Yields Same Result -
Statement 1 :

i[str] = 'A';

Statement 2 :

str[i] = 'A';

Statement 3 :

*(i+str) = 'A';

Statement 4 :

*(str+i) = 'A';

For More Information - Refer This Article [ How Array is Randomly Accessed i.e How i[a] or a[i] Works ? ]