Page 1 of 2

something i bet you didnt know you could do with pointers.

Posted: Fri Dec 05, 2008 3:55 pm
by avansc
pointers to functions are very powerful.
you can pass a function as a parameter- this is probably one of the coolest things you can to with any programming language

Code: Select all

#include <stdio.h>
#include <stdlib.h>

int add(int x, int y);
int sub(int x, int y);
int calc(int x, int y, int(*func)(int, int));

int add(int x, int y)
{
	return x+y;
}

int sub(int x, int y)
{
	return x-y;
}

int calc(int x, int y, int(*func)(int, int))
{
	return func(x, y);
}

int main(void)
{

	int (*fp1)(int, int) = add;
	int (*fp2)(int, int) = sub;

	printf("add via function poiter : %d\n", calc(2,1,fp1));
	printf("sub via function poiter : %d\n", calc(2,1,fp2));
	getchar();
	return 0;
}

Re: something i bet you didnt know you could do with pointers.

Posted: Fri Dec 05, 2008 4:06 pm
by Falco Girgis
Yeah, that is so badass. It's also straight C without any C++icism.

Re: something i bet you didnt know you could do with pointers.

Posted: Fri Dec 05, 2008 4:16 pm
by M_D_K
Used them manys a time.

I had them in an old version of a quake style in-game console.

Re: something i bet you didnt know you could do with pointers.

Posted: Fri Dec 05, 2008 10:31 pm
by trufun202
Those are known as Delegates in the C# world. They're great for asynchronous callbacks.

Delegates were actually the basis for my idea for the menu-driven conversations in my game. Except it's Lua, so I just passed a string of the function name, rather than a pointer.

Re: something i bet you didnt know you could do with pointers.

Posted: Sat Dec 06, 2008 12:53 am
by MarauderIIC
Or, if you need flexibility when it comes to the parameters, you could use a functionoid
http://en.wikipedia.org/wiki/Functionoid

Re: something i bet you didnt know you could do with pointers.

Posted: Sat Dec 06, 2008 12:50 pm
by avansc
MarauderIIC wrote:Or, if you need flexibility when it comes to the parameters, you could use a functionoid
http://en.wikipedia.org/wiki/Functionoid

a functionoid is just another name for a function.

function =
proc
procedure
method
functor
functionoid

that wiki page does not really have any usefull info on it.

Re: something i bet you didnt know you could do with pointers.

Posted: Sat Dec 06, 2008 5:13 pm
by MarauderIIC
Well, sort of. Quick illustration in C++ (to do the same thing in C, I think you have to use macros?)

Code: Select all

class Functionoid {
public:
    int sharedparam;
    virtual bool operator()() = 0;
};

class Fn1 : public Functionoid {
public
    int param1;
    int param2;
    virtual bool operator()();
};

class Fn2 : public Functionoid {
public:
    char param3;
    char param4;
    virtual bool operator()();
};

Code: Select all

main() {
    Fn1 fn1;
    fn1.param1 = fn1.param2 = fn1.sharedparam = 1;
    Fn2 fn2;
    fn2.param3 = fn2.param4 ='a';
    fn2.sharedparam = 2;
    doStuff(fn1);
    doStuff(fn2);
}
void doStuff(Functionoid& fn) {
    fn();
}
Effectively use a fn as a parameter but the fns you use don't have to have the same parameters.

Re: something i bet you didnt know you could do with pointers.

Posted: Sat Dec 06, 2008 5:16 pm
by avansc
MarauderIIC wrote:Well, sort of. Quick illustration-

Code: Select all

class Functionoid {
public:
    int sharedparam;
    virtual bool operator()() = 0;
};

class Fn1 : public Functionoid {
public
    int param1;
    int param2;
    virtual bool operator()();
};

class Fn2 : public Functionoid {
public:
    char param3;
    char param4;
    virtual bool operator()();
};

Code: Select all

main() {
    Fn1 fn1;
    fn1.param1 = fn1.param2 = fn1.sharedparam = 1;
    Fn2 fn2;
    fn2.param3 = fn2.param4 ='a';
    fn2.sharedparam = 2;
    doStuff(fn1);
    doStuff(fn2);
}
void doStuff(Functionoid& fn) {
    fn();
}
i see, but thats not strickly a function. its a class.

Re: something i bet you didnt know you could do with pointers.

Posted: Sat Dec 06, 2008 5:17 pm
by MarauderIIC
Effectively acts as a function (another name for functionoid is function object). :)
Edited that a few times ^

Re: something i bet you didnt know you could do with pointers.

Posted: Sat Dec 06, 2008 5:19 pm
by MarauderIIC
Also it's the only way, that I know of, to do such a thing: pass functions as parameters, where the functions themselves don't have to have the same parameter types.

Re: something i bet you didnt know you could do with pointers.

Posted: Sat Dec 06, 2008 5:20 pm
by avansc
MarauderIIC wrote:Also it's the only way, that I know of, to do such a thing: pass functions as parameters, where the functions themselves don't have to have the same parameter types.

no you can do that with out making a class for it

Re: something i bet you didnt know you could do with pointers.

Posted: Sat Dec 06, 2008 5:26 pm
by MarauderIIC
Besides C-macros, which I mentioned in the first post but forgot to mention in the other one, how?

Re: something i bet you didnt know you could do with pointers.

Posted: Sat Dec 06, 2008 5:34 pm
by avansc
MarauderIIC wrote:Besides C-macros, which I mentioned in the first post but forgot to mention in the other one, how?
"pass functions as parameters, where the functions themselves don't have to have the same parameter types."

rephrase that a bit so i understand so i dont make a fool out of my self

Re: something i bet you didnt know you could do with pointers.

Posted: Sun Dec 07, 2008 9:47 pm
by MarauderIIC
Sure.
avansc wrote:int calc(int x, int y, int(*func)(int, int));
You're stuck passing something that takes two ints.

Code: Select all

bool do(Functionoid& fn) { //or Functionoid* if you want
   return fn();
}
You're not stuck passing something that takes two ints.

Main:
Click here to see the hidden message (It might contain spoilers)
int main() {
Fn1 fn1;
fn1.param1 = 1;
fn1.param2 = 2;
fn1.sharedparam = 3;
Fn2 fn2;
fn2.sharedparam = 3;
fn2.param3 = 'a';
fn2.param4 = 'b';
do(fn1);
do(fn2);
}[/code]

See, I'm passing a function (object) as a parameter but the function (objects) don't have to have the same parameter types. One's parameters are ints, one's parameters are chars. Could do strings or something that can't be cast to an int, of course.

And pasted from my last post, the class details:
Click here to see the hidden message (It might contain spoilers)

Code: Select all

class Functionoid {
public:
    int sharedparam;
    virtual bool operator()() = 0;
};

class Fn1 : public Functionoid {
public
    int param1;
    int param2;
    virtual bool operator()();
};

class Fn2 : public Functionoid {
public:
    char param3;
    char param4;
    virtual bool operator()();
};


Alternatively you could probably add parameters to the () operator or one of the constructors; this is just a simple example though.

Re: something i bet you didnt know you could do with pointers.

Posted: Sun Dec 07, 2008 10:03 pm
by bugmenot
@MarauderIIC: That is not a functor as the parameters are stored before the functor is given to code that is invoking the function. This article should explain the uses of a functor: http://elysianshadows.com/phpBB3/viewto ... art=999999

Edit: Wrong url: http://en.wikipedia.org/wiki/Function_object