arrow
arrow
arrow
Consider the following code: #include<stdio.h> void f1(char *x, char *y) { char *t1; t1 = x; x = y; y = t1;} void f2(char **x, char
Question



Consider the following code:
#include<stdio.h>
void f1(char *x, char *y) {
char *t1; t1 = x; x = y; y = t1;}
void f2(char **x, char **y) {
char *t1; t1 = *x; *x = *y; *y = t1;}
int main()
{
char *a = “ONE”, *b = “TWO”;
f1(a, b); printf(“%s %s”, a, b);
f2(&a, &b); printf(“%s %s”, a, b);
return 0;
}
What will be the output of the above code?

A.

ONE TWO TWO ONE

B.

TWO ONE ONE TWO

C.

ONE TWO ONE TWO

D.

TWO ONE TWO ONE

Correct option is A

The f1 function attempts to swap values locally within its scope, having no effect on a and b. However, f2 swaps the pointers by dereferencing them, effectively changing the values of a and b in main().
· First printf: f1 has no effect, so output remains as ONE TWO.
· Second printf: f2 swaps the values to TWO ONE.
Information Booster:
· Pointer Functions: Demonstrates pass-by-value vs. pointer dereferencing.
· Local Swap Limitation: f1 only affects local copies, not original pointers.

Free Tests

Free
Must Attempt

Basics of Education: Pedagogy, Andragogy, and Hutagogy

languageIcon English
  • pdpQsnIcon10 Questions
  • pdpsheetsIcon20 Marks
  • timerIcon12 Mins
languageIcon English
Free
Must Attempt

UGC NET Paper 1 Mock Test 1

languageIcon English
  • pdpQsnIcon50 Questions
  • pdpsheetsIcon100 Marks
  • timerIcon60 Mins
languageIcon English
Free
Must Attempt

Basics of Education: Pedagogy, Andragogy, and Hutagogy

languageIcon English
  • pdpQsnIcon10 Questions
  • pdpsheetsIcon20 Marks
  • timerIcon12 Mins
languageIcon English

Similar Questions

test-prime-package

Access ‘UGC NET Computer Science’ Mock Tests with

  • 60000+ Mocks and Previous Year Papers
  • Unlimited Re-Attempts
  • Personalised Report Card
  • 500% Refund on Final Selection
  • Largest Community
students-icon
354k+ students have already unlocked exclusive benefits with Test Prime!
Our Plans
Monthsup-arrow