home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c      Meh, in C you gotta define EVERYTHING      243,242 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 242,718 of 243,242   
   Ben Bacarisse to highcrew   
   Re: function pointer question   
   03 Jan 26 03:33:54   
   
   From: ben@bsb.me.uk   
      
   highcrew  writes:   
      
   > On 1/2/26 3:43 PM, Michael Sanders wrote:   
   >> (It's also common to define a typedef for a   
   >> pointer-to-function type, but I prefer to typedef the function type   
   >> itself   
   >   
   > OK, that's crazy! I didn't know of this!   
   >   
   > It seems a bit weird at first, but I like the type consistency:   
   > you declare `typedef int callback(int)` and declare `callback *c`   
   > making it very explicit that we are talking of a pointer.   
   >   
   > On the other hand it is not clear what `callback c` should do,   
   > but it makes sense as soon as you declare it.   
      
   Declarations using the function type do often make sense.  Note   
   "declarations" -- you can't use the typedef'd name to /define/ a function   
   of the given type.   
      
   A pattern I've used more than once when setting up a table of function   
   pointers that act like op-codes.  Maybe you have an add function, a sub   
   function, a mul functions and a div function.  These are all defined in   
   a file arithmetic.c, but the table (maybe in another file) needs to see   
   declarations of the names:   
      
   typedef double operation(double, double);   
   /* ... */   
      
   extern operation add, sub, mul, div;   
      
   static struct {   
       char *name;   
       operation *function;   
   } ops[] = {   
       { "add",      add },   
       { "subtract", sub },   
       { "multiply", mul },   
       { "divide",   div }   
   };   
      
   --   
   Ben.   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca