Machine-translated by Google. Questions are authored in English.
Top

Discussion

What will be the output of the program ?

#include <stdio.h>
void swap(char *, char *);

int main()
{
    char *pstr[2] = {"Hello", "IndiaBIX"};
    swap(pstr[0], pstr[1]);
    printf("%s\n%s", pstr[0], pstr[1]);
    return 0;
}
void swap(char *t1, char *t2)
{
    char *t;
    t=t1;
    t1=t2;
    t2=t;
}

 

  • A.IndiaBIX
    Hello
  • B.Address of "Hello" and "IndiaBIX"
  • C.Hello
    IndiaBIX
  • D.Iello
    HndiaBIX

Answer: C

Correct answer: Hello IndiaBIX

swap receives copies of the two pointers and exchanges the copies, which vanish when the function returns. The array in main is untouched, so the strings print in their original order. To swap the caller’s pointers the function would have to take char ** and be called as swap(&pstr[0], &pstr[1]).

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.