Groups | Search | Server Info | Login | Register
Groups > comp.lang.c > #396294
| From | Tim Rentsch <tr.17687@z991.linuxsc.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: function pointer question |
| Date | 2026-01-07 21:52 -0800 |
| Organization | A noiseless patient Spider |
| Message-ID | <86h5swodm7.fsf@linuxsc.com> (permalink) |
| References | (3 earlier) <10j8r9n$ha1t$1@dont-email.me> <87ms2v8j4t.fsf@bsb.me.uk> <10jbdbd$1b3h1$1@dont-email.me> <20260106123503.456@kylheku.com> <10jltg8$nqav$1@dont-email.me> |
Andrey Tarasevich <noone@noone.net> writes:
> On Tue 1/6/2026 12:41 PM, Kaz Kylheku wrote:
>
>>>> typedef double operation(double, double);
>>>> /* ... */
>>>>
>>>> extern operation add, sub, mul, div;
>>>>
>>>> static struct {
>>>> char *name;
>>>> operation *function;
>>>> } ops[] = {
>>>> { "add", add },
>>>> { "subtract", sub },
>>>> { "multiply", mul },
>>>> { "divide", div }
>>>> };
>>>
>>> ... with a remark that `extern` is completely redundant here.
>>>
>>> operation add, sub, mul, div;
>>
>> Butlonly because operation is a function type, so the concept of
>> a tentative definition doesn't apply.
>
> Yes, to me it feels like it has way less potential to mislead with an
> extern`. But I can't really say whether this perception is objectively
> inherent in the construct, or it is just the fact that it is an exotic
> way of declaring functions (for me and, probably, for most people).
>
> However, while it is true that the concept of a tentative definition
> doesn't apply, I still don't quite get your point. What if were an
> object type and the concept would apply? Are you implying that
> tentative definitions should be avoided (i.e. that all object
> definitions should include an initializer)?
If we ignore "static" for the moment, there is a simple rule:
use 'extern' for declarations, and nothing for definitions.
This rule works for both functions and objects.
Now if we add "static" back into the mix, and limit the discussion
to functions, the same rule applies provided we stipulate that
everything is pre-declared. Thus, always use 'extern' or 'static'
to declare (in advance) a function, and on the function definition
don't use any storage class.
Unfortunately, the way 'static' works for objects is not symmetric.
There is no way to write a non-defining declaration for a static
object. And, what is worse, once an object has been declared (and
so tentatively defined) with 'static', then any definition must also
use 'static'. Hence we have a new rule: always use 'extern' or
'static' when declaring a function or object, and leave off both
when defining a function or object, /except/ 'static' must be used
when defining a static object. C would have been nicer if 'static'
for objects worked the same way as 'static' for functions. Oh well.
>> The lack of extern could trip someone up who refactors the
>> code such that the operations are object types:
>>
>> named_operation add, sub, mul, div; // oops, multiple definition
>>
>> static named_operation ops[] = { add, sub, mul, div };
>
> Again, I'm at a bit of a loss. What is this intended to illustrate?
To me the example looks flawed (and not because of multiple
definitions). My advice is to stick with the rule described above
for declarations and definitions.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar
function pointer question Michael Sanders <porkchop@invalid.foo> - 2026-01-02 07:24 +0000
Re: function pointer question Lawrence D’Oliveiro <ldo@nz.invalid> - 2026-01-02 09:04 +0000
Re: function pointer question Michael Sanders <porkchop@invalid.foo> - 2026-01-02 14:42 +0000
Re: function pointer question Michael Sanders <porkchop@invalid.foo> - 2026-01-02 14:45 +0000
Re: function pointer question Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2026-01-02 02:52 -0800
Re: function pointer question Michael Sanders <porkchop@invalid.foo> - 2026-01-02 14:43 +0000
Re: function pointer question highcrew <high.crew3868@fastmail.com> - 2026-01-02 17:21 +0100
Re: function pointer question Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2026-01-02 09:37 -0800
Re: function pointer question Ben Bacarisse <ben@bsb.me.uk> - 2026-01-03 03:33 +0000
Re: function pointer question Andrey Tarasevich <noone@noone.net> - 2026-01-03 07:41 -0800
Re: function pointer question Lawrence D’Oliveiro <ldo@nz.invalid> - 2026-01-03 21:46 +0000
Re: function pointer question David Brown <david.brown@hesbynett.no> - 2026-01-04 12:03 +0100
Re: function pointer question Kaz Kylheku <046-301-5902@kylheku.com> - 2026-01-06 20:41 +0000
Re: function pointer question Andrey Tarasevich <noone@noone.net> - 2026-01-07 07:18 -0800
Re: function pointer question Tim Rentsch <tr.17687@z991.linuxsc.com> - 2026-01-07 21:52 -0800
Re: function pointer question David Brown <david.brown@hesbynett.no> - 2026-01-08 09:17 +0100
Re: function pointer question Kaz Kylheku <046-301-5902@kylheku.com> - 2026-01-02 17:48 +0000
Re: function pointer question Michael Sanders <porkchop@invalid.foo> - 2026-01-02 19:35 +0000
Re: function pointer question Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2026-01-02 12:07 -0800
Re: function pointer question Michael Sanders <porkchop@invalid.foo> - 2026-01-03 06:06 +0000
Re: function pointer question Kaz Kylheku <046-301-5902@kylheku.com> - 2026-01-02 21:50 +0000
Re: function pointer question Kaz Kylheku <046-301-5902@kylheku.com> - 2026-01-02 21:52 +0000
Re: function pointer question "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2026-01-02 14:18 -0800
Re: function pointer question David Brown <david.brown@hesbynett.no> - 2026-01-03 13:55 +0100
Re: function pointer question "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2026-01-03 12:04 -0800
Re: function pointer question Andrey Tarasevich <noone@noone.net> - 2026-01-03 13:01 -0800
Re: function pointer question Tim Rentsch <tr.17687@z991.linuxsc.com> - 2026-01-07 07:35 -0800
Re: function pointer question Andrey Tarasevich <noone@noone.net> - 2026-01-07 08:17 -0800
Re: function pointer question Andrey Tarasevich <noone@noone.net> - 2026-01-07 08:23 -0800
Re: function pointer question Tim Rentsch <tr.17687@z991.linuxsc.com> - 2026-01-07 18:44 -0800
Re: function pointer question Tim Rentsch <tr.17687@z991.linuxsc.com> - 2026-01-07 18:27 -0800
Re: function pointer question Lawrence D’Oliveiro <ldo@nz.invalid> - 2026-01-03 22:05 +0000
Re: function pointer question Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2026-01-03 16:39 -0800
Re: function pointer question David Brown <david.brown@hesbynett.no> - 2026-01-04 12:15 +0100
Re: function pointer question Kaz Kylheku <046-301-5902@kylheku.com> - 2026-01-06 20:33 +0000
Re: function pointer question Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2026-01-06 17:01 -0800
Re: function pointer question Michael Sanders <porkchop@invalid.foo> - 2026-01-03 06:08 +0000
Re: function pointer question "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2026-01-05 12:40 -0800
Re: function pointer question Michael Sanders <porkchop@invalid.foo> - 2026-01-06 04:30 +0000
Re: function pointer question "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2026-01-06 17:05 -0800
Re: function pointer question James Kuyper <jameskuyper@alumni.caltech.edu> - 2026-01-03 17:20 -0500
Re: function pointer question Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2026-01-03 16:48 -0800
Re: function pointer question Michael Sanders <porkchop@invalid.foo> - 2026-01-05 08:39 +0000
Re: function pointer question Michael Sanders <porkchop@invalid.foo> - 2026-01-06 12:32 +0000
Re: function pointer question highcrew <high.crew3868@fastmail.com> - 2026-01-06 13:59 +0100
Re: function pointer question Michael Sanders <porkchop@invalid.foo> - 2026-01-06 13:57 +0000
Re: function pointer question antispam@fricas.org (Waldek Hebisch) - 2026-01-06 14:50 +0000
Re: function pointer question highcrew <high.crew3868@fastmail.com> - 2026-01-06 21:44 +0100
Re: function pointer question scott@slp53.sl.home (Scott Lurndal) - 2026-01-06 22:08 +0000
Re: function pointer question Tim Rentsch <tr.17687@z991.linuxsc.com> - 2026-01-07 05:59 -0800
Re: function pointer question antispam@fricas.org (Waldek Hebisch) - 2026-01-07 09:25 +0000
Re: function pointer question David Brown <david.brown@hesbynett.no> - 2026-01-07 11:37 +0100
Re: function pointer question Michael S <already5chosen@yahoo.com> - 2026-01-06 15:47 +0200
Re: function pointer question Michael Sanders <porkchop@invalid.foo> - 2026-01-06 14:01 +0000
Re: function pointer question David Brown <david.brown@hesbynett.no> - 2026-01-06 15:55 +0100
Re: function pointer question Michael Sanders <porkchop@invalid.foo> - 2026-01-06 16:44 +0000
Re: function pointer question scott@slp53.sl.home (Scott Lurndal) - 2026-01-06 15:41 +0000
Re: function pointer question Michael Sanders <porkchop@invalid.foo> - 2026-01-06 16:45 +0000
Re: function pointer question James Kuyper <jameskuyper@alumni.caltech.edu> - 2026-01-06 10:58 -0500
Re: function pointer question Michael Sanders <porkchop@invalid.foo> - 2026-01-06 16:49 +0000
Re: function pointer question James Kuyper <jameskuyper@alumni.caltech.edu> - 2026-01-06 12:09 -0500
Re: function pointer question Michael Sanders <porkchop@invalid.foo> - 2026-01-07 21:18 +0000
Re: function pointer question Tim Rentsch <tr.17687@z991.linuxsc.com> - 2026-01-09 09:14 -0800
Re: function pointer question Andrey Tarasevich <noone@noone.net> - 2026-01-10 19:17 -0800
Re: function pointer question "James Russell Kuyper Jr." <jameskuyper@alumni.caltech.edu> - 2026-01-10 22:39 -0500
Re: function pointer question Tim Rentsch <tr.17687@z991.linuxsc.com> - 2026-01-11 11:49 -0800
Re: function pointer question James Kuyper <jameskuyper@alumni.caltech.edu> - 2026-01-05 06:47 -0500
Re: function pointer question James Kuyper <jameskuyper@alumni.caltech.edu> - 2026-01-02 14:03 -0500
Re: function pointer question Michael Sanders <porkchop@invalid.foo> - 2026-01-02 19:41 +0000
Re: function pointer question bart <bc@freeuk.com> - 2026-01-02 19:18 +0000
Re: function pointer question Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2026-01-02 11:43 -0800
Re: function pointer question Michael Sanders <porkchop@invalid.foo> - 2026-01-02 19:44 +0000
csiph-web