:: Re: [DNG] Making sense of C pointer…
Top Page
Delete this message
Reply to this message
Author: Steve Litt
Date:  
To: dng
Subject: Re: [DNG] Making sense of C pointer syntax.
On Mon, 28 Mar 2016 12:28:03 +0200 (CEST)
karl@??? wrote:

> To exemplify the "as they are used" statement, take a function
> pointer declaration:
>
> void (*log_func)(int priority, const char *format);
>
> here you cannot conveniently move the "*" to the "void" so it will
> look like a "pointer" declaration; it declares log_func to be
> something which if used as (*log_func)(a, b, d) will "give" you a
> void value.


Years ago I gave up trying to logically explain the syntax of function
pointers, which are so essential for callbacks and pseudo-oop, and just
memorized the idiom.

Edition 1 of K&R had an actual algorithm by which one could dissect any
lvalue (thing that can appear on the left of the equal sign), but it
was so complicated I couldn't understand it. So I memorized the idiom
for function pointers.

I think C would have been much more successful (and it's already been
quite successful) if it could have had a better syntax for function
pointers. I think the reason you see so few callback functions in
average C code is the syntax, as well as the unforgivingly strict typing
of the arguments.

Some time compare what it takes to do a callback in C compared to
Python, or especially Lua.

SteveT

Steve Litt
March 2016 featured book: Quit Joblessness: Start Your Own Business
http://www.troubleshooters.com/startbiz