Ricky Hussmann wrote:
{Quote hidden}> ----- Original Message -----
> From: "Ashley Roll" <
spamBeGoneashspamBeGone
DIGITALNEMESIS.COM>
> To: <
TakeThisOuTPICLISTEraseME
spam_OUTMITVMA.MIT.EDU>
> Sent: Wednesday, December 11, 2002 1:18 AM
> Subject: Re: function pointer syntax in C/C++
>
> > Hi Brendan
> >
> > The "nicest" way is to use typedefs:
> >
> > int foo(int bar); // function to point to0
> >
> > typedef int (* MyFuncPtrType)(int /* parameters here */);
> >
> > Then you can say:
> >
> > MyFuncPtrType pFunc = &foo;
>
> I'm not quite sure this is right. As far as I know, you don't need the "&"
> before the function name. It may still work, I haven't tried it.
>
You are correct b'cos the compiler is able to determine that this is only a address for the function
and not a value
example
int i = 5;
int *x = &i;
or
int i = 5;
int *x;
x = &5;
the variable i is the value "5" while the &i represents the address.
{Quote hidden}>
> I'm basically adapting the following from my C reference manual:
>
> /* This is the function prototype */
> int foo(int);
>
> /* This is will be the pointer to our function above, called p */
> int (*p) (int);
>
> /* This is a variable to hold the return value of the function */
> int answer;
>
> /* This will assign the address of the function foo to p */
> /* The function name itself is actually a pointer to the function */
> p = foo;
>
> /* Now using the function pointer we can call the function foo */
> answer = (*p) (4);
>
Suprisingly GCC allows
answer = (p)(4); // not sure that this is apart of the ANSI C standard
{Quote hidden}>
> Look at the above function pointer declaration, the first word is "int".
> This is the return type of your function. Next is (*p). This is the name of
> the variable, you can call it whatever you want. We could have said int
> (*foo_p) (int) if we wanted, its just the variable name.
>
> The final part are the parameters of the function. Say we had another
> function we wanted to make a pointer to called foo2, and say it looked like:
>
> int foo2(int, int, float);
>
> Then the declaration of the pointer variable would look like this:
>
> int (*foo2_p) (int, int, float);
>
> We would then assign the pointer like this:
>
> foo2_p = foo2;
>
> And we could call the function like this:
>
> answer = (*foo2_p) (1, 2, 3.0);
>
> Again, foo2_p is just the name of the variable, but you can call it whatever
> you want. This is the way defined by the ANSI C standard, and is guaranteed
> to work. Ash's answer may still be correct, I didn't check.
>
> I really hope this helps. Good luck in your coding.
>
> Ricky Hussmann
>
> > > {Original Message removed}