in Programming retagged by
2 votes
2 votes

What will be the output of the following program?

void swap(char **s1, char **s2){
    char *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
in Programming 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


Related questions