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 15:50 -0400 |
| Organization | Self |
| Message-ID | <553A9E7E.5020207@verizon.net> (permalink) |
| References | (4 earlier) <6ac6f8b2-cf77-4112-94ac-f5f0d8d82060@googlegroups.com> <55397EAE.1040009@verizon.net> <fab5222e-49e5-4458-a5d0-4fa69c4c6d14@googlegroups.com> <553A8909.2060901@verizon.net> <8e2b93dc-cbbe-415d-bdaf-2463bc2aa45d@googlegroups.com> |
On 04/24/2015 03:36 PM, samuelallenalexander@gmail.com wrote:
> On Friday, April 24, 2015 at 7:18:58 PM UTC+1, James Kuyper wrote:
...
>> 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.
>
> Thanks kindly. There's no compelling reason not to do what you suggested, I just wish it were unncessary because it makes the 'corresponding functions' more opaque, that's all. With a particular form of opacity that isn't easy to whisk away with the preprocessor. (It also causes unnecessary suppression of compiler warnings, e.g., if a user accidentally passes a struct one_int to print_to_ints, your suggestion will compile without complaint.)
That's a reason why accessing the function through a pointer stored with
the data can be a good idea when writing object-oriented code in C.
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