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.
Pratik said:
1 decade ago
aftr swapping the element how does it provide this output
Natraj said:
1 decade ago
I<=4 MEANS how fourth name alone exchanged to fifth name.
Renju said:
1 decade ago
char *t;
t = names[3];
names[3] = names[4]
names[4] = names[3]
what I did not understand here is, t is pointing to the address of names[3].
So if we change the content in the address of names[3], t should then point to this new value right.
So at the end.. shouldn't names[3] and names[4] be same (which is "Baiju" in this case)?
Can anyone please clear this doubt?
t = names[3];
names[3] = names[4]
names[4] = names[3]
what I did not understand here is, t is pointing to the address of names[3].
So if we change the content in the address of names[3], t should then point to this new value right.
So at the end.. shouldn't names[3] and names[4] be same (which is "Baiju" in this case)?
Can anyone please clear this doubt?
Srinivas said:
1 decade ago
Why it is Ritu, Baiju-->3, 4th positions?
We just changed the pointing positions not the values.
It should be Baji Baji.
We just changed the pointing positions not the values.
It should be Baji Baji.
Atul said:
1 decade ago
String cannot be assigned to any other string, so it will generate error.
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.
Lusi said:
1 decade ago
How names[3] and names[4] are converted to names[4]and names[5] respectively?
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.
Tara said:
10 years ago
Since the data is present in code section we cannot modify the contents. So this generates the error.
Pushpender said:
9 years ago
Nice Explantation @Chetan.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers