Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Casting function pointers to different signatures |
| Date | 2015-04-24 14:18 -0400 |
| Organization | Self |
| Message-ID | <553A8909.2060901@verizon.net> (permalink) |
| References | (2 earlier) <ed979da3-069e-4ccb-a99d-951a7fa34ae2@googlegroups.com> <55396C7E.3070403@verizon.net> <6ac6f8b2-cf77-4112-94ac-f5f0d8d82060@googlegroups.com> <55397EAE.1040009@verizon.net> <fab5222e-49e5-4458-a5d0-4fa69c4c6d14@googlegroups.com> |
On 04/24/2015 01:42 PM, samuelallenalexander@gmail.com wrote:
> On Friday, April 24, 2015 at 12:22:30 AM UTC+1, James Kuyper wrote:
>> On 04/23/2015 06:49 PM, Sam wrote:
>>> On Thursday, April 23, 2015 at 11:04:53 PM UTC+1, James Kuyper wrote:
>>>> On 04/23/2015 05:39 PM, Sam wrote:
>> ...
>>>>> Thanks for the very detailed answer with references and everything.
>>>>>
>>>>> It's really too bad the behavior is undefined since it would open the door for porting some genuinely useful OOP/FP idioms into C.
>>>>
>>>> How is the door closed? It's trivial to make the function types
>>>> compatible, and if you do, the code will work. Could you give an example
>>>> of code using one of those idioms where it's excessively difficult to
>>>> avoid the problem? Your current example does NOT qualify.
>>>
>>> Yes, you could make the individual printer functions have signature
>>> void (*) (void*), but that makes those individual printer functions a
>>> lot more opaque on their own, and it's very repetitive that you have
>>> to repeat the casts over and over.
>>
>> That comment suggests a possible misunderstanding. If the function types
>> were, in fact, compatible, there would be no need for a cast, the
>> function would already have the correct type.
>
> Suppose I have structures of various sizes, and functions to print them. For example,
>
> struct an_int
> {
> int x;
> };
>
> struct two_ints
> {
> int x;
> iny y;
> };
>
> void print_an_int( struct an_int *p )
> {
> printf( "%d\n", p->x );
> }
>
> void print_two_ints( struct two_ints *p )
> {
> printf( "%d %d\n", p->x, p->y );
> }
>
> If buf is a NULL-terminated array of struct an_int's, I'd like to be able to do
>
> print_array( print_an_int, buf );
>
> If buf is a NULL-terminated array of struct two_ints's, I'd like to be able to do
>
> print_array( print_two_ints, buf );
>
>
> Is there a way to do this without changing print_an_int or print_two_ints themselves?
No - my suggestion involved changing the corresponding functions:
void print_an_int(void *pv)
{
struct an_int *p = pv;
...
}
If there's a reason why you can't do that, you'll need to explain that
reason. If it's a valid reason, we might be able to suggest an
alternative approach that will work.
Note that in contexts like these, it's more typical for object-oriented
C code to access the appropriate function through a pointer stored with
the data it's designed to work on, so it's easier to make sure that it's
the correct function. Implementations of C++ based on C typically use a
vtable pointer for this purpose, though there are other ways to do it.
If you're familiar with the concept, I recommend considering using it in
this context. If you're unfamiliar with the concept, I can explain further.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Casting function pointers to different signatures samuelallenalexander@gmail.com - 2015-04-23 13:39 -0700
Re: Casting function pointers to different signatures Barry Schwarz <schwarzb@dqel.com> - 2015-04-23 14:01 -0700
Re: Casting function pointers to different signatures James Kuyper <jameskuyper@verizon.net> - 2015-04-23 17:10 -0400
Re: Casting function pointers to different signatures samuelallenalexander@gmail.com - 2015-04-23 14:39 -0700
Re: Casting function pointers to different signatures James Kuyper <jameskuyper@verizon.net> - 2015-04-23 18:04 -0400
Re: Casting function pointers to different signatures samuelallenalexander@gmail.com - 2015-04-23 15:49 -0700
Re: Casting function pointers to different signatures James Kuyper <jameskuyper@verizon.net> - 2015-04-23 19:22 -0400
Re: Casting function pointers to different signatures samuelallenalexander@gmail.com - 2015-04-24 10:42 -0700
Re: Casting function pointers to different signatures James Kuyper <jameskuyper@verizon.net> - 2015-04-24 14:18 -0400
Re: Casting function pointers to different signatures samuelallenalexander@gmail.com - 2015-04-24 12:36 -0700
Re: Casting function pointers to different signatures James Kuyper <jameskuyper@verizon.net> - 2015-04-24 15:50 -0400
Re: Casting function pointers to different signatures Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-04-24 13:18 -0700
Re: Casting function pointers to different signatures Siri Cruz <chine.bleu@yahoo.com> - 2015-04-23 16:08 -0700
Re: Casting function pointers to different signatures Keith Thompson <kst-u@mib.org> - 2015-04-23 18:36 -0700
Re: Casting function pointers to different signatures Siri Cruz <chine.bleu@yahoo.com> - 2015-04-23 19:00 -0700
Re: Casting function pointers to different signatures raltbos@xs4all.nl (Richard Bos) - 2015-04-25 10:48 +0000
Re: Casting function pointers to different signatures Tim Rentsch <txr@alumni.caltech.edu> - 2015-04-25 04:05 -0700
Re: Casting function pointers to different signatures Lőrinczy Zsigmond <nospam@for.me> - 2015-04-24 06:51 +0200
Re: Casting function pointers to different signatures Richard Damon <Richard@Damon-Family.org> - 2015-04-25 10:57 -0400
csiph-web