• edited by
26,045 views
78 78 votes
#include<stdio.h>
void fun1(char* s1, char* s2){
    char* temp;
    temp = s1;
    s1 = s2;
    s2 = temp;
}
void fun2(char** s1, char** s2){
    char* temp;
    temp = *s1;
    *s1 = *s2;
    *s2 = temp;
}
int main(){
    char *str1="Hi", *str2 = "Bye";
    fun1(str1, str2); printf("%s %s", str1, str2);
    fun2(&str1, &str2); printf("%s %s", str1, str2);
    return 0;
}

The output of the program above is:

  1. $\text{Hi Bye Bye Hi}$
  2. $\text{Hi Bye Hi Bye}$
  3. $\text{Bye Hi Hi Bye}$
  4. $\text{Bye Hi Bye Hi}$

7 Answers

Best answer
71 71 votes
func1(char* s1, char* s2){
    char* temp;
    temp = s1;
    s1 = s2;
    s2 = temp;
}

Everything is local here. So, once function completes its execution all modification go in vain.

func2(char** s1, char** s2){
    char* temp
    temp = *s1
    *s1 = *s2
    *s2 = temp
}

This will retain modification and swap pointers of string.
So output would be Hi Bye Bye Hi

Correct Answer: $A$

• edited by
15 15 votes

The first call to the function 'func1(str1,str2);' is Call by Value. Hence, any change in the formal parameters are NOT reflected in actual parameters. Hence, str1 points at "hi" and str2 points at "bye".

The second call to the function 'func2(&str1,&str2);' is Call by Reference. Hence, any change in the formal parameters are reflected in actual parameters. Hence, str1 now points at "bye" and str2 points at "hi".

Hence, answer is hi byebye hi ....

1 1 vote
The first call to the function ‘func1(str1, str2);’ is call by value.
Hence, any change in the formal parameters are NOT reflected in actual parameters.
Hence, str1 points at “hi” and str2 points at “bye”.
The second call to the function ‘func2(&str1, &str2);’ is call by reference.
Hence, any change in formal parameters are reflected in actual parameters.
Hence, str1 now points at “bye” and str2 points at “hi”.

Hence answer is “hi bye bye hi”.
1 1 vote

The correct answer is (A).

i.e. Hi Bye Bye Hi

In short, the fun1 function attempts to swap the pointers s1 and s2, but since C uses pass-by-value, the swap is local to fun1 and doesn't affect the pointers in main. Thus, it performs a "fake swap" and has no effect on the output.

In the second function fun2, pointers to pointers are used as arguments (char**). This allows the function to modify the original pointers str1 and str2 from main by dereferencing them. Therefore, when fun2 swaps the values pointed to by s1 and s2, it directly affects str1 and str2 in main. This results in a successful swap of the strings "Hi" and "Bye", which is reflected in the output.

0 0 votes
Str1 and Str2 are Constant Pointers
and Base Address of the Constant Pointers Cannot get Modified
Str1 and Str2 will remains the same
Answer:
Position:
Show:

Related questions

4 4 votes
1 1 answer
1.3k
1.3k views
GO Classes asked Apr 30, 2022
1,270 views
#include<stdio.h int test(int *a, int *b) { int c = *a-*b; if (c<0) return 0; else return (1 + test(&c, b)); } void main() { int x = 15; int y = 4; int a = test(&x,&y); p...
56 56 votes
7 answers 7 answers
29.0k
29.0k views
gatecse asked Feb 14, 2018
28,969 views
Consider the following C code. Assume that unsigned long int type length is $64$ bits.unsigned long int fun(unsigned long int n) { unsigned long int i, j=0, sum = 0; for(...
89 89 votes
11 answers 11 answers
37.7k
37.7k views
gatecse asked Feb 14, 2018
37,673 views
Consider the following C program:#include<stdio.h struct Ournode{ char x, y, z; }; int main() { struct Ournode p={'1', '0', 'a'+2}; struct Ournode *q=&p; printf("%c, %c",...
69 69 votes
6 answers 6 answers
26.3k
26.3k views
Kathleen asked Sep 12, 2014
26,324 views
What is printed by the following C program?int f(int x, int *py, int ppz) { int y, z; ppz += 1; z = ppz; // corrected z = *ppz; to z = ppz; *py += 2; y = *py; x += 3;...