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

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

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

Post 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;
}
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

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

Post by Falco Girgis »

Yeah, that is so badass. It's also straight C without any C++icism.
User avatar
M_D_K
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1087
Joined: Tue Oct 28, 2008 10:33 am
Favorite Gaming Platforms: PC
Programming Language of Choice: C/++
Location: UK

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

Post by M_D_K »

Used them manys a time.

I had them in an old version of a quake style in-game console.
Gyro Sheen wrote:you pour their inventory onto my life
IRC wrote: <sparda> The routine had a stack overflow, sorry.
<sparda> Apparently the stack was full of shit.
User avatar
trufun202
Game Developer
Game Developer
Posts: 1105
Joined: Sun Sep 21, 2008 12:27 am
Location: Dallas, TX
Contact:

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

Post 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.
-Chris

YouTube | Twitter | Rad Raygun

“REAL ARTISTS SHIP” - Steve Jobs
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

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

Post by MarauderIIC »

Or, if you need flexibility when it comes to the parameters, you could use a functionoid
http://en.wikipedia.org/wiki/Functionoid
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

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

Post 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.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

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

Post 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.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

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

Post 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.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

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

Post by MarauderIIC »

Effectively acts as a function (another name for functionoid is function object). :)
Edited that a few times ^
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

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

Post 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.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

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

Post 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
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

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

Post by MarauderIIC »

Besides C-macros, which I mentioned in the first post but forgot to mention in the other one, how?
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

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

Post 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
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

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

Post 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.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
bugmenot
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 62
Joined: Sun Dec 07, 2008 7:05 pm

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

Post 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
Last edited by bugmenot on Sun Dec 07, 2008 10:12 pm, edited 1 time in total.
Post Reply