retagged by
567 views
4 votes
4 votes

What will be the output of the following program?

#include<stdio.h>
void swap(char **s1, char **s2){
    char *tmp;
    tmp=*s1;
    *s1=*s2;
    *s2=tmp;
}
int main()
{
    char *str[3]= {"orange", "apple", "pear"};
    for (int i = 0; i<2; i++)
        swap(&strs[i], &strs[i+1]);
    printf("%s %s %s", strs[0], strs[1], strs[2]);
}
  1. pear apple orange
  2. apple pear orange
  3. orange apple pear
  4. apple orange pea
retagged by

1 Answer

5 votes
5 votes

Answer: B

*str[3] is an array of pointers and it is like

After 1st iteration of the for loop content of str[0] and str[1] is swapped. It will be like

After 2nd iteration of for loop content of str[1] and str[2] is swapped. It will be like

Therefore it is printed in following order:

apple pear orange

Answer:

Related questions

5 votes
5 votes
1 answer
1