Output is 4 how ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Output is 4 how ?

#include <iostream> using namespace std; typedef int (*fpointer)(int,int); int f1(int x,int y){return x+y;} int f2(int x,int y){return x-y;} fpointer getFunc(int x){ if(x==1) return f1; if(x==2) return f2; } int main() { cout<<getFunc(1)(2,2); return 0; }

26th Sep 2017, 12:58 PM
Anuj kumar
Anuj kumar - avatar
6 Answers
+ 13
cout << getFunc(1)(2, 2); getFunc(1) => x == 1 is true => f1 is returned. cout << f1(2, 2); f1(2, 2) => 2 + 2 = 4 is returned. So, 4 is the output.
26th Sep 2017, 1:17 PM
Krishna Teja Yeluripati
Krishna Teja Yeluripati - avatar
+ 2
@krishna Teja yeluripati thanks
26th Sep 2017, 1:19 PM
Anuj kumar
Anuj kumar - avatar
+ 2
your name is too long ...
26th Sep 2017, 1:19 PM
Anuj kumar
Anuj kumar - avatar
+ 2
#include <iostream> using namespace std; int *fun(){ return new int[2]; } int fun(int*p){ delete[]p; return 0; } void fun (int*p,int q){ p[q]*=2; } void fun(int*p,int q,int r){ p[q]=r; } int main() { int *v=fun(); fun(v,0,1); fun(v,1,2); fun(v,0); cout<<v[1]+v[0]; fun(v); return 0; } this program is giving error please find output
26th Sep 2017, 1:22 PM
Anuj kumar
Anuj kumar - avatar
+ 1
~swim~ wow awesome very nice explain thank-you very much
1st Oct 2017, 6:16 PM
Anuj kumar
Anuj kumar - avatar
0
~swim~ i fixed error now please tell me how to come output 4?
1st Oct 2017, 5:51 PM
Anuj kumar
Anuj kumar - avatar