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,704 of 243,242   
   James Kuyper to Michael Sanders   
   Re: function pointer question   
   02 Jan 26 14:03:48   
   
   From: jameskuyper@alumni.caltech.edu   
      
   On 2026-01-02 02:24, Michael Sanders wrote:   
   > i have:   
   >   
   > void moo(char HISTORY[][64], int hst_len, int invalid, const char   
   > *gme_msg)   
   >   
   > void mastermind(char HISTORY[][64], int hst_len, int invalid, const   
   > char *gme_msg)   
   >   
   > to use either i have:   
   >   
   > void (*render)(char [][64], int, int, const char *) = MOO ? moo :   
   > mastermind;   
   >   
   > my multi-part question:   
   >   
   > why is void required for the function pointer?   
      
   Because otherwise you would have to cast each function to match the type   
   of the function pointer before assigning it.   
      
   > A: because both moo() & mastermind return void?   
      
   You can safely convert any function pointer to any other function   
   pointer type. However, you must convert it back to a a pointer to a   
   function type that is compatible with the function itself before using   
   the pointer to call that function.   
   The simplest way to deal with that fact is to make sure that all the   
   functions that you might want the pointer to point at have the same   
   type, and then just declare it as a pointer to that type.   
   The more complicated approach requires you to keep track in some fashion   
   what the original function type was, and to convert it back to that type   
   before dereferencing it. There are rare situations where this is worth   
   doing, but in general you should avoid this approach.   
      
   > B: because every function must have a return type   
   > *including function pointers*?   
      
   Well, that's true, but what I said about compatibility of a function   
   pointer with a function's actual type is the more important issue, and   
   implies what you've said above.   
      
   > C: what about tyedef?   
      
   You'll need to explain what you your question about typedef is. Here's   
   the key thing you need to know: a typedef does not declare a new type,   
   it merely provides an alias for a type. If you replace every occurrence   
   of a typedef with the type it's a defined as an alias of, your code   
   should work exactly the same.   
      
   --- 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