C Programming - Strings - Discussion
Discussion Forum : Strings - Find Output of Program (Q.No. 26)
26.
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;
}
Answer: Option
Explanation:
Step 1: void swap(char *, char *); This prototype tells the compiler that the function swap accept two strings as arguments and it does not return anything.
Step 2: char *pstr[2] = {"Hello", "IndiaBIX"}; The variable pstr is declared as an pointer to the array of strings. It is initialized to
pstr[0] = "Hello", pstr[1] = "IndiaBIX"
Step 3: swap(pstr[0], pstr[1]); The swap function is called by "call by value". Hence it does not affect the output of the program.
If the swap function is "called by reference" it will affect the variable pstr.
Step 4: printf("%s\n%s", pstr[0], pstr[1]); It prints the value of pstr[0] and pstr[1].
Hence the output of the program is
Hello
IndiaBIX
Discussion:
10 comments Page 1 of 1.
Chetan Agarwal said:
10 years ago
No people the explanation is wrong if the value is call by value, then any change of value t in the function swap should not affect the value of strings pstr[0] and pstr[2] in main.
Try the below code:
#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;
*t1='3';
*t2='4';
}
In Turbo C it works to give output as 4ello 3ndiabix. This proves that the transfer is call by reference only.
The reason there is no actual swapping done in the original code is that we are just swapping the new pointers (t1 and t2) who are pointing to the original strings and not the actual pointers who are pointing to them pstr[0] and pstr[1].
Its like we already had pstr[0] and pstr[1], and now we assigned t1 and t2 respectively and then we discarded t1 and t2 by doing this it will not affect the value of pstr[0] and pstr[2].
Try the below code:
#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;
*t1='3';
*t2='4';
}
In Turbo C it works to give output as 4ello 3ndiabix. This proves that the transfer is call by reference only.
The reason there is no actual swapping done in the original code is that we are just swapping the new pointers (t1 and t2) who are pointing to the original strings and not the actual pointers who are pointing to them pstr[0] and pstr[1].
Its like we already had pstr[0] and pstr[1], and now we assigned t1 and t2 respectively and then we discarded t1 and t2 by doing this it will not affect the value of pstr[0] and pstr[2].
Kamal nayan said:
1 decade ago
Actually we are passing here value of the pointers & catching with single * variable,hence due to call by value its not affecting the original value of variable, in stead of it if we will pass the address of pointer @ catch with ** pointer variable,then due to call by reference the original variable will be affected and swaping will be come in light,by that way swap also done but its not look because change is not done in original variable.
this the one of the way to do swap with affect in original variable.
#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;
}
this the one of the way to do swap with affect in original variable.
#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;
}
(1)
Utkarsh Gupta said:
6 years ago
@All.
According to my knowledge, we can write it as;
#include <stdio.h>
void swap(char **, char **);
int main()
{
char *name = "Utkarsh";
char *title = "Gupta";
swap(&name, &title);
printf("%s\n%s\n", name, title);
return 0;
}
The reason there is no actual swapping done in the original code is that we are just swapping the new pointers (t1 and t2) who are pointing to the original strings and not the actual pointers who are pointing to them pstr[0] and pstr[1].
void swap(char **a, char **b)
{
char *temp;
temp = *a;
*a = *b;
*b = temp;
}
According to my knowledge, we can write it as;
#include <stdio.h>
void swap(char **, char **);
int main()
{
char *name = "Utkarsh";
char *title = "Gupta";
swap(&name, &title);
printf("%s\n%s\n", name, title);
return 0;
}
The reason there is no actual swapping done in the original code is that we are just swapping the new pointers (t1 and t2) who are pointing to the original strings and not the actual pointers who are pointing to them pstr[0] and pstr[1].
void swap(char **a, char **b)
{
char *temp;
temp = *a;
*a = *b;
*b = temp;
}
(1)
Rupinderjit said:
1 decade ago
Since,pstr[2] is array of pointers to strings.So two pointer points to two strings.When pstr[0] & pstr[1] are passed ,it passes values pointed to by both pointers.
Run this
#include<stdio.h>
void swap(char *, char *);
int main()
{
char *pstr[2] = {"Hello", "IndiaBIX"};
printf("%s\n%s", pstr[0], pstr[1]);
return 0;
}
It will print the values not an addresses.
Run this
#include<stdio.h>
void swap(char *, char *);
int main()
{
char *pstr[2] = {"Hello", "IndiaBIX"};
printf("%s\n%s", pstr[0], pstr[1]);
return 0;
}
It will print the values not an addresses.
OMKAR GURAV said:
10 years ago
How come the following code works as call by reference then?
#include<stdio.h>
void fun(int *p);
int main()
{
int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 8, 7, 8, 9, 0};
int *ptr;
ptr = &a[0][0];
fun(ptr);
printf("%d\n", *ptr);
return 0;
}
void fun(int *p)
{
++*p;
printf("%d\n", *p);
}
#include<stdio.h>
void fun(int *p);
int main()
{
int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 8, 7, 8, 9, 0};
int *ptr;
ptr = &a[0][0];
fun(ptr);
printf("%d\n", *ptr);
return 0;
}
void fun(int *p)
{
++*p;
printf("%d\n", *p);
}
Bhupesh said:
1 decade ago
Some1 please tell me the syntax of calling this swap function by reference so that the string "Hello" and "IndiaBIX" get actually swaped. Me waiting eagerly.
Mits said:
1 decade ago
What is call by value and call by reference?
Soumya said:
1 decade ago
@bhupesh
swap(&pstr[0],&pstr[1]);
swap(&pstr[0],&pstr[1]);
Robert said:
9 years ago
Good job @Kamal Nayan.
Raju said:
3 years ago
Thanks @Kamal Nayan.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers