#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;
}
#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 main still prints ONE TWO. f2 receives the addresses of a and b and swaps the pointers themselves, so the second print is TWO ONE.
More Programming Languages and Computer Graphics questions
- #include<iostream.h> using namespace std; void swap(int &x, int &y) { int temp=x; x=y; y=temp; } int main() { int a=5, b=10; swap(a,b); swa…
- 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…
- #include<stdio.h> void main() { int arr[ ] = {1, 2, 3, 4, 5}; int *p = arr; printf("%d", *p++); printf("%d", *(p+1)); } Find the output of…
- Which of the following is not an object Oriented Programming language ?
- Multiple Inheritance is permitted directly in..... A. C++ B. Java C. Python D. VB.NET E. BASIC
- Match List-I with List-II :