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 listserv@mitvma.mit.edu with SET PICList DIGEST in the body