C Programming - Strings - Discussion
Discussion Forum : Strings - Find Output of Program (Q.No. 9)
9.
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: Option
Explanation:
Step 1: char *names[] = { "Suresh", "Siva", "Sona", "Baiju", "Ritu"}; The variable names is declared as an pointer to a array of strings.
Step 2: int i; The variable i is declared as an integer type.
Step 3: char *t; The variable t is declared as pointer to a string.
Step 4: t = names[3]; names[3] = names[4]; names[4] = t; These statements the swaps the 4 and 5 element of the array names.
Step 5: for(i=0; i<=4; i++) printf("%s,", names[i]); These statement prints the all the value of the array names.
Hence the output of the program is "Suresh, Siva, Sona, Ritu, Baiju".
Discussion:
14 comments Page 1 of 2.
Helan said:
7 years ago
@Chetan.
Your explanation is very understandable in a simple way. Thanks.
Your explanation is very understandable in a simple way. Thanks.
Ereina said:
8 years ago
Thank you @Chetan. I understand well.
Reshma K said:
8 years ago
Thank you @Chetan.
Priyanka said:
8 years ago
Thanks @Chetan.
Pushpender said:
9 years ago
Nice Explantation @Chetan.
Tara said:
10 years ago
Since the data is present in code section we cannot modify the contents. So this generates the error.
Chetan Agarwal said:
10 years ago
Step 1: char *names[] = {"Suresh", "Siva", "Sona", "Baiju", "Ritu"};\.
Explanation: This is not the array of characters but instead this is the array of the pointers containing address of the string "Suresh", "Siva", "Sona", "Baiju", "Ritu" and let the address of these strings be 100, 200, 300, 400, 500 respectively.
Now the tricky part is to understand that *name[] is NOT an array of characters its an array of POINTERS TO CHARACTERS.
Means its just an array of pointers who are pointer to different strings.
*name[0] = 100.
*name[1] = 200.
*name[2] = 300.
*name[3] = 400.
*name[4] = 500.
So when we do this T = name[3]; it means t will be pointing to a string Baiju which is also pointed by name[3] at address 400;
Name[3] = name[4]; it means name[3] will be pointing to a string Ritu which is also pointed by name[4] at address 500;
Name[4] = t; it means name[4] will be pointing to a string Baiju which is also pointed by t at address 400; So the swapping is done this way.
Explanation: This is not the array of characters but instead this is the array of the pointers containing address of the string "Suresh", "Siva", "Sona", "Baiju", "Ritu" and let the address of these strings be 100, 200, 300, 400, 500 respectively.
Now the tricky part is to understand that *name[] is NOT an array of characters its an array of POINTERS TO CHARACTERS.
Means its just an array of pointers who are pointer to different strings.
*name[0] = 100.
*name[1] = 200.
*name[2] = 300.
*name[3] = 400.
*name[4] = 500.
So when we do this T = name[3]; it means t will be pointing to a string Baiju which is also pointed by name[3] at address 400;
Name[3] = name[4]; it means name[3] will be pointing to a string Ritu which is also pointed by name[4] at address 500;
Name[4] = t; it means name[4] will be pointing to a string Baiju which is also pointed by t at address 400; So the swapping is done this way.
Lusi said:
1 decade ago
How names[3] and names[4] are converted to names[4]and names[5] respectively?
Ankita said:
1 decade ago
names[3] contain the base address of the 4th name.
Here we are assigning names[3], the base address of names[4].
That is not possible..We cannot change the base address.
Here we are assigning names[3], the base address of names[4].
That is not possible..We cannot change the base address.
Atul said:
1 decade ago
String cannot be assigned to any other string, so it will generate error.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers