• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: declaring a function whose arg is another function
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: declaring a function whose arg is another function


  • Subject: Re: declaring a function whose arg is another function
  • From: Alastair Houghton <email@hidden>
  • Date: Mon, 24 Sep 2007 15:48:46 +0100

On 24 Sep 2007, at 12:29, Roland Silver wrote:

I have a problem having to do with the right C-code for declaring and calling a function which has another function as argument.
I've tried to find the answer in Harbison/Steele, without success.


Consider a function like foo:

void foo(int arg);

and another C-function, bar, that has as argument a function like foo, and returns nothing.

Question 1: how do I declare bar? One of these ways?
void bar(void fcn(int));
void bar(void (*fcn)(int));
void bar(void (*fcn)());

The middle one is best; the top one won't work at all, and the bottom one means different things in C and C++. But people normally write


  typedef void (*callback_fn_t)(int arg);

and then do

  void bar (callback_fn_t callback);

because (a) it's easier to read (often easier to type too), and (b) you sometimes find yourself wanting to cast your callback function (e.g. because of minor type differences).

Question 2: how do I call bar with foo as argument? Like one of these?
bar(foo);
bar(&foo)

You don't need the "&". Likewise, you don't need to write e.g. (*callback)(5)... you can just do callback(5). Pointer-to-function variables are generally interchangeable with function names (similar to the way that pointer variables are generally interchangeable with array names).


Question 3: Is this the right list to post this kind of question? If not, where?

Not really, no. comp.lang.c is the canonical place for C questions (which is what this is), or you could try a more general purpose list (e.g. Omni's macosx-dev list).


This should really be obvious... just ask yourself: Is this question about *Cocoa*? If the answer is "No", then this is not the right mailing list.

Kind regards,

Alastair.

--
http://alastairs-place.net


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >declaring a function whose arg is another function (From: Roland Silver <email@hidden>)

  • Prev by Date: Re: NSArrayController question
  • Next by Date: Re: NSMutableArray + NSEnumerator = No Memory
  • Previous by thread: declaring a function whose arg is another function
  • Next by thread: Closing application from NSStatusBar menu?
  • Index(es):
    • Date
    • Thread