help me with void pointers

Can you explain me this code line by line with detailed explanation?
and when should i start learning win api? im have covered the tutorial in this site to pointers. i have little understanding in classes, thats all. when should i learn win api??
here is the code:
//explain me line by line
// pointer to functions
#include <iostream>
using namespace std;

int addition (int a, int b)
{ return (a+b); }

int subtraction (int a, int b)
{ return (a-b); }

int operation (int x, int y, int (*functocall)(int,int))
{
int g;
g = (*functocall)(x,y);
return (g);
}

int main ()
{
int m,n;
int (*minus)(int,int) = subtraction;

m = operation (7, 5, addition);
n = operation (20, m, minus);
cout <<n;
return 0;
}
Thanks.
Mr. Puff
What are you struggling with exactly?

There are no void pointers here. There are function pointers but they're different.
a simple search of this site would have produced this:

http://www.cplusplus.com/forum/beginner/105731/
i dont understand please explain me everything pls
HELP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using namespace std;

int addition (int a, int b)
{ return (a+b); }

int subtraction (int a, int b)
{ return (a-b); }

int operation (int x, int y, int (*functocall)(int,int))
{
    int g;
    g = (*functocall)(x,y);
    return (g);
}

int main ()
{
    int m,n;
    int (*minus)(int,int) = subtraction;

    m = operation (7, 5, addition);
    n = operation (20, m, minus);
    cout <<n;
    return 0;
}


main()
19: declare two variables of type int
20: declare one variable of type pointer to a function that looks like int func(int, int). Initialise it with address of function subtraction.
22: call function operation, pass parameters 7, 5, address of function addition
23: call function operation, pass parameters 20, m, minus (which is the address of subtraction)

operation()
10: operation is a function that takes 3 parameters, x of type int, y of type int, functocall of type pointer to a function that looks like int func(int, int)
12: declare variable g of type int
13: call the function who's address in in local variable functocall, with parameters x, y and save the result in g
This really should be:
 
g = functocall(x,y);

14: return g
Last edited on
Explaining that code line-by-line is a waste of time.

For example, I'm assuming you know what's happening in this line:
int m,n;

Why don't you specify exactly what you're stuck on. That way, you'll get a better suited answer. Is it the function pointer syntax? The function pointer itself?
line 10 -15
and how you pass paramets to that function and that function and what is null pointers what is so good using null pointers? what is (*minus)?
i know what is v(void *) its casting no?


explain this code too!! what parameters passed to functions??
#include<iostream.h>

void swap(int &a,int &b);

int main()
{
int num1=5,num2=10;
swap(num1,num2);
cout << num1 << endl << num2;
return 0;
}

void swap(int &a,int &b)
{
int temp;
temp=a;
a=b;
b=temp;
}
Last edited on
line 10 -15
and how you pass paramets to that function
I already show you.
 
g = functocall(x,y);


what is null pointers what is so good using null pointers?
A pointer is a variable that holds a memory location. So it can point to some value or not. It's conventional to assign it the value NULL to indicate that the pointer isn't pointing at anything.

what is (*minus)?
Nothing. I take it your talking about int (*minus)(int,int)
I explained that in detail above. I think iHutch105 might have been right.

its casting no?
No.

explain this code too!!
No. The function pointer stuff was tricky, but that's pretty basic stuff. If you don't understand that code, you need to read a tutorial.
Last edited on
Topic archived. No new replies allowed.