What will be the output of the program ?
#include <stdio.h>
int main()
{
char *names[] = { "Suresh", "Siva", "Sona", "Baiju", "Ritu"};
int i;
char *t;
t = names[3];
names[3] = names[4];
names[4] = t;
for(i=0; i<=4; i++)
printf("%s,", names[i]);
return 0;
}
Answer: B
Correct answer: Suresh, Siva, Sona, Ritu, Baiju
The array holds pointers, and the code exchanges two of them through the temporary t - the strings themselves are never touched or copied. So elements 3 and 4 swap places, giving Suresh, Siva, Sona, Ritu, Baiju. Swapping pointers rather than character arrays is exactly why an array of pointers is preferred for sorting strings.
Please log in or register to report this question.