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,845 of 243,242   
   Kaz Kylheku to Andrey Tarasevich   
   Re: function pointer question   
   06 Jan 26 20:41:34   
   
   From: 046-301-5902@kylheku.com   
      
   On 2026-01-03, Andrey Tarasevich  wrote:   
   > On Fri 1/2/2026 7:33 PM, Ben Bacarisse wrote:   
   >>   
   >> 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 }   
   >> };   
   >>   
   >   
   > ... with a remark that `extern` is completely redundant here.   
   >   
   >    operation add, sub, mul, div;   
      
   Butlonly because operation is a function type, so the concept of   
   a tentative definition doesn't apply.   
      
   The lack of extern could trip someone up who refactors the   
   code such that the operations are object types:   
      
     named_operation add, sub, mul, div; // oops, multiple definition   
      
     static named_operation ops[] = { add, sub, mul, div };   
      
      
   --   
   TXR Programming Language: http://nongnu.org/txr   
   Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal   
   Mastodon: @Kazinator@mstdn.ca   
      
   --- 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