Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > *generic* pointer to function(s)?

Reply
Thread Tools

*generic* pointer to function(s)?

 
 
Malcolm McLean
Guest
Posts: n/a
 
      12-04-2007
<(E-Mail Removed)> wrote in message
> I am interested in having multiple functions with different prototypes
> and deciding, by setting a pointer, which of them to use later in the
> program.
>
> Eg:
>
> int f1(void);
> char* f2(int);
> /* ... many more of these to choose from */
>

Don't go down this route.
Building an argument list at runtime is one of the very few operations that
C will not allow you to do.
You can cast to and from void (*fptr)(void) s and any other type on almost
every architecture, but that avails you little if you are then going to
hardcode the calls. You might as well do the job properly and use a union.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

 
Reply With Quote
 
 
 
 
Flash Gordon
Guest
Posts: n/a
 
      12-04-2007
Malcolm McLean wrote, On 04/12/07 22:06:
> <(E-Mail Removed)> wrote in message
>> I am interested in having multiple functions with different prototypes
>> and deciding, by setting a pointer, which of them to use later in the
>> program.
>>
>> Eg:
>>
>> int f1(void);
>> char* f2(int);
>> /* ... many more of these to choose from */
>>

> Don't go down this route.
> Building an argument list at runtime is one of the very few operations
> that C will not allow you to do.


True.

> You can cast to and from void (*fptr)(void) s and any other type on
> almost every architecture,


You can do it on *every* architecture that has a C compiler because the
C standard says you can do it.

> but that avails you little if you are then
> going to hardcode the calls. You might as well do the job properly and
> use a union.


Whether a union buys you anything will depend on the exact situation.
--
Flash Gordon
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer to an array vs pointer to pointer subramanian100in@yahoo.com, India C Programming 5 09-23-2011 10:28 AM
Pointer to pointer or reference to pointer A C++ 7 07-05-2011 07:49 PM
Pointer to pointer Vs References to Pointer bansalvikrant@gmail.com C++ 4 07-02-2009 10:20 AM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM



Advertisments