Moving into some larger problems now since the little ones are getting a little boring Remember NO COPYING & PASTING into your compiler etc to run it. Just read the code and figure out what it does.
Consider the following program
Code:
void(*p[2])(int *,int);
void f1(int* p, int q)
{
int tmp;
tmp = *p;
*p = q;
q = tmp;
}
void f2(int* p, int q)
{
int tmp;
tmp = *p;
*p = q;
q = tmp;
}
void main()
{
int a;
int b;
p[0] = f1;
p[1] = f2;
a = 3;
b = 5;
p[0](&a, b);
printf("%d\t %d\t", a, b);
p[1](&a, b);
printf("%d\t %d\t", a, b);
}
What is the output it generates?
the output might be
<!--5 3 3 5-->
I havent actually gotten aroud to learning pointers to functions yet but i think i understand them.
p[0] and [1] will point to f1 and f2(which are both the same function o__O)
parameter p is a pointer(so it will be able to modify the inputted variable outside the function.
parameter q however cannot modify the inputted variable outside the function.
whtever p points to is made to contain q, and q is made what p was, but since only p can carry the value outside the function, a is made equal to b, but b stays unchanged.
edit: Must say this one was pretty easy for me since i use function pointers almost daily when detouring ^^
Originally Posted by Hell_Demon
the output is 5 5 5 5
p[0] and [1] will point to f1 and f2(which are both the same function o__O)
parameter p is a pointer(so it will be able to modify the inputted variable outside the function.
parameter q however cannot modify the inputted variable outside the function.
whtever p points to is made to contain q, and q is made what p was, but since only p can carry the value outside the function, a is made equal to b, but b stays unchanged.
edit: Must say this one was pretty easy for me since i use function pointers almost daily when detouring ^^
What is detouring? And why am i only 50% befriended T_T
Originally Posted by zeco
What is detouring? And why am i only 50% befriended T_T
Redirecting functions through your own, so if the game calls a, it will go though your function a first(so you can modify parameters, or not return the original at all )
and ur 50% befriended because i dont feel we are 100% friends yet :P
Originally Posted by Hell_Demon
Redirecting functions through your own, so if the game calls a, it will go though your function a first(so you can modify parameters, or not return the original at all )
and ur 50% befriended because i dont feel we are 100% friends yet :P
Hmm interesting. Tell me more about it sometime =)
If you've never seen a function pointer before this may have confused you a little, but it's really quite simple.
We basically say that 'p' is an array of 2 function pointers and it can point to methods with a signature of void(int*, int);
Then we have 2 methods defined called 'f1' and 'f2' which indeed do the same thing. Now at first glance these seem to be a classic 'swap the values' method, however the first parameter is passed by reference, and the 2nd isn't.
As you know (or so I hope when dealing with variable scope) when you pass in an int to a method it actually is working with a copy of it, so that if you say increment the value in the method then when you return the value is still the original value it was before. When passing in a value by reference though you're saying: work with the value at this address which then DOES alter the original.
Knowing that, we call the first function which if you follow the flow ends up putting 'b' in 'a', but 'b' doesn't get the value of 'a'. So we print out "5 5".
Then when we call it again (but now through f2 which is exactly the same as f1) it does the same thing, sticks b(5) into a and that "5 5"