Groups | Search | Server Info | Login | Register


Groups > comp.lang.c > #396130

Re: function pointer question

From Keith Thompson <Keith.S.Thompson+u@gmail.com>
Newsgroups comp.lang.c
Subject Re: function pointer question
Date 2026-01-03 16:39 -0800
Organization None to speak of
Message-ID <875x9i43f1.fsf@example.invalid> (permalink)
References (1 earlier) <20260102091518.226@kylheku.com> <10j96mn$jrsp$1@dont-email.me> <10j9enb$p6ts$2@dont-email.me> <10j9g71$pr9o$1@dont-email.me> <10jb3j5$17gcb$3@dont-email.me>

Show all headers | View raw


David Brown <david.brown@hesbynett.no> writes:
> On 02/01/2026 23:18, Chris M. Thomasson wrote:
[...]
>> typedef void (*generic_func_ptr)(void)
>
> There is no generic function pointer type equivalent to "void *" - you
> are always going to need casts when converting between different
> pointer types.  And you /really/ need to make sure you convert to
> exactly the correct type before calling the pointed-to function.  But
> the definition you gave here is commonly used when people want to have
> generic function pointers.

Yes, but ...

There is no generic function pointer type equivalent to void* in the
sense that values can be converted to and from the type implicitly.
For example:

    int n = 42;
    void *vp = &n;   // implicit conversion
    double *dp = vp; // implicit conversion, potentially dangerous

There are no such implicit conversions for function pointer types.

However, in the sense of a round-trip conversion yielding the
original result *all* function pointer types can be treated as
generic function pointer types.  You just have to use casts to do
any conversions.

It's very common for all pointer types in a given implementation
to have the same representation, and for round-trip conversions
to safely yield the original value, but it's not guaranteed by
the language, and there are implementations where, for example,
function pointer types are bigger than object pointer types, or
void* is bigger than int*.  Certain collections of pointer types
are guaranteed by the language to have the same representation:

- void*, char*, signed char*, unsigned char*
- All struct pointer types
- All union pointer types
- All function pointer types

Incidentally, if I want a generic function pointer type, I might
define it so that it can't be used accidentally without a cast.
For example:

    typedef struct dummy__ (generic_function)(void);

where struct dummy__ is an incomplete type.  (Note that I've
typedef'ed the function type, not the pointer type.)  But that
might be considered overkill; treating a void function with
no parameters as generic is probably good enough.

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

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