What is the output of the following program?#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;
}
Pick an option to check yourself, or open the answer below.
Show answer & explanation
Correct answer: A. ONE TWO TWO ONE
Explanation
f1 swaps only its local copies of the pointers, so a and b are unchanged: 'ONE TWO'. f2 receives the addresses of a and b and really swaps them: 'TWO ONE'.More Programming Languages and Computer Graphics questions
- Which of the following C++ statements correctly declares an abstract class?
- Consider the following C++ program:#include<iostream> using namespace std; void swap(int &x, int &y) { int temp = x; x = y; y = temp; } int…
- What is the output of the code given below?#include<iostream> using namespace std; class Base { public: Base() { cout << "Base Const"; } vi…
- Consider the following C code:#include<stdio.h> int temp = 0; int fun(int x, int y) { int z; temp++; if (y == 3) return (x*x*x); else { z =…
- Match List I with List IIList I List II (A) HTML (I) Allows for dynamic and interactive web pages. (B) DHTML (II) Defines the structure of…
- Arrange the following steps of file handling in C in the correct order: (A) Close the file (B) Read from or write to the file (C) Open the…