Searching \ for 'function pointer syntax in C/C++' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/language/index.htm?key=c%2B%2B
Search entire site for: 'function pointer syntax in C/C++'.

Truncated match.
PICList Thread
'function pointer syntax in C/C++'
2002\12\11@010724 by Brendan Moran

flavicon
face
On most C syntax I do just fine, but function pointers are always a
head-scratcher for me.

If I wanted to declare a pointer to this function:

int foo(int bar);

What would be the syntax for the declaration?

The best I came up with was this:

(int)(*)(int) pfoo = &foo;

but that doesn't compile.

Thanks,
--Brendan

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email spam_OUTlistservTakeThisOuTspammitvma.mit.edu with SET PICList DIGEST in the body


2002\12\11@011903 by Ashley Roll

flavicon
face
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;

Hope that helps..
Cheers,
Ash.

---
Ashley Roll
Digital Nemesis Pty Ltd
http://www.digitalnemesis.com
Mobile: +61 (0)417 705 718




> {Original Message removed}

2002\12\11@015437 by Ricky Hussmann

flavicon
face
----- Original Message -----
From: "Ashley Roll" <.....ashKILLspamspam@spam@DIGITALNEMESIS.COM>
To: <PICLISTspamKILLspamMITVMA.MIT.EDU>
Sent: Wednesday, December 11, 2002 1:18 AM
Subject: Re: function pointer syntax in C/C++


{Quote hidden}

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.

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);

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}

2002\12\11@022415 by Ashley Roll

flavicon
face
Hi Ricky,

Interesting, I just tried both your way and mine and they seem to work. I've
always just done it the way I said..

#include <stdio.h>

typedef int (*FooPtrType)(int);

int foo(int bar)
{
       printf("Foo: %d\r\n", bar);
       return 0;
}

main()
{
       FooPtrType pFoo1 = foo;
       FooPtrType pFoo2 = &foo;

       (*pFoo1)(1);
       pFoo2(2);
}

I like to use mine because it makes it explicit that you are taking the
address of the function.

Also there is no need to dereference the pointer when calling the function.
This just makes reading the code that much more difficult. Assuming you name
you pointers in a distinct way.. Guess its just preferences.

Cheers,
Ash.

---
Ashley Roll
Digital Nemesis Pty Ltd
http://www.digitalnemesis.com
Mobile: +61 (0)417 705 718

> {Original Message removed}

2002\12\11@030243 by Brendan Moran
flavicon
face
> > 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.

Thanks for that, guys, it works well.  As to the '&', I'm not sure if it's
accepted, but it works fine without it.

--Brendan

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email .....listservKILLspamspam.....mitvma.mit.edu with SET PICList DIGEST in the body


2002\12\11@081956 by Bob Ammerman

picon face
A pointer to the function:

int foo(int bar)

can be declared simply:

int (*myFooPtr)(int);

I often choose to create a typedef:

typedef int (*fooptr_t)(int);

then I can:

fooptr_t    myFooPtr;
fooptr_t    yourFooPtr;

Bob Ammerman
RAm Systems

{Original Message removed}

2002\12\11@082411 by Bob Ammerman

picon face
The "C" standard says that a standalone function name (without trailing
parens) 'degenerates' to the address of the function.

So,

myFooPtr = foo;

and

myFooPtr = &foo;

do the same thing in ANSI C.

Bob Ammerman
RAm Systems

----- Original Message -----
From: "Ricky Hussmann" <EraseMEgargoylespam_OUTspamTakeThisOuTIOLINC.NET>
To: <PICLISTspamspam_OUTMITVMA.MIT.EDU>
Sent: Wednesday, December 11, 2002 1:41 AM
Subject: Re: function pointer syntax in C/C++


{Quote hidden}

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:
{Quote hidden}

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}

2002\12\11@134122 by Byron A Jeff

face picon face
On Tue, Dec 10, 2002 at 09:40:36PM -0800, Brendan Moran wrote:
> On most C syntax I do just fine, but function pointers are always a
> head-scratcher for me.
>
> If I wanted to declare a pointer to this function:
>
> int foo(int bar);
>
> What would be the syntax for the declaration?
>
> The best I came up with was this:
>
> (int)(*)(int) pfoo = &foo;
>
> but that doesn't compile.

It's actually very simple once you realize that the function () have the
highest precedence. Couple that with the fact that you want to define a
function pointer, and you're in business.

Start be defining all of the elements:

int *pfoo(int) = foo; // Note that the & is redundant in front of foo

This won't compile but is the correct format. The reason is precedence (i.e.
5+3*2 is 11 not 16). pfoo binds to the function () first so pfoo is a function
that returns an int pointer.

So to change the precedence add parenthesis. pfoo is
first an foremost a pointer, so put parens around the * and pfoo so that it
binds first.

int (*pfoo)(int) = foo; // Works every time!

It's just that simple. pfoo must now be a pointer above all else and since
it has function parens following it's now a pointer to a function.

BAJ

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email RemoveMElistservTakeThisOuTspammitvma.mit.edu with SET PICList DIGEST in the body


2002\12\11@234325 by johnc

flavicon
face
Ricky Hussmann wrote:

{Quote hidden}

  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}

   Suprisingly GCC allows
   answer = (p)(4); // not sure that this is apart of the ANSI C standard

{Quote hidden}

> > > {Original Message removed}

More... (looser matching)
- Last day of these posts
- In 2002 , 2003 only
- Today
- New search...