Skip to content
Related Articles

Related Articles

GATE | GATE-IT-2004 | Question 61

View Discussion
Improve Article
Save Article
  • Difficulty Level : Expert
  • Last Updated : 28 Jun, 2021

Consider the following C program:




#include <stdio.h>
typedef struct 
{
    char *a;
    char *b;
} t;
void f1(t s);
void f2(t *p);
main()
{
    static t s = {"A", "B"};
    printf ("%s %s\n", s.a, s.b);
    f1(s);
    printf ("%s %s\n", s.a, s.b);
    f2(&s);
}
void f1(t s)
{
    s.a = "U";
    s.b = "V";
    printf ("%s %s\n", s.a, s.b);
    return;
}
void f2(t *p)
{
    p -> a  = "V";
    p -> b = "W";
    printf("%s %s\n", p -> a, p -> b);
    return;
}


What is the output generated by the program ?
(A) A B
U V
V W
V W
(B) A B
U V
A B
V W
(C) A B
U V
U V
V W
(D) A B
U V
V W
U V


Answer: (B)

Explanation:

The value of  structure s is passed by value in f1().   In f2(), address of s is passed.   Therefore any  changes made in f1() don’t reflect in main() , but changes made in f2() reflect.

 printf (“%s %s\n”, s.a, s.b);   //  print local static values of a and b;A B

f1(s); give call to printf (“%s %s\n”, s.a, s.b); //print local values of a and b ;U V

printf (“%s %s\n”, s.a, s.b);  //  print local static values of a and b;A B

f2(&s);-> printf(“%s %s\n”, p -> a, p -> b); // gives present contents at memory location i.e U V

Therefore answer is B See the for code solution at: https://ide.geeksforgeeks.org/q36urV

Quiz of this Question


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!