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" To: Sent: Wednesday, December 11, 2002 1:41 AM Subject: Re: function pointer syntax in C/C++ > ----- Original Message ----- > From: "Ashley Roll" > To: > 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. > > 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----- > > > From: pic microcontroller discussion list > > > [mailto:PICLIST@MITVMA.MIT.EDU]On Behalf Of Brendan Moran > > > Sent: Wednesday, 11 December 2002 3:41 PM > > > To: PICLIST@MITVMA.MIT.EDU > > > Subject: function pointer syntax in C/C++ > > > > > > > > > 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); > > > > -- > > http://www.piclist.com#nomail Going offline? Don't AutoReply us! > > email listserv@mitvma.mit.edu with SET PICList DIGEST in the body > > > > > > > > > > -- > http://www.piclist.com#nomail Going offline? Don't AutoReply us! > email listserv@mitvma.mit.edu with SET PICList DIGEST in the body > > -- http://www.piclist.com#nomail Going offline? Don't AutoReply us! email listserv@mitvma.mit.edu with SET PICList DIGEST in the body