Groups | Search | Server Info | Login | Register


Groups > comp.lang.c > #396088

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-02 12:07 -0800
Organization None to speak of
Message-ID <87v7hju6c9.fsf@example.invalid> (permalink)
References <10j7rs6$7c9e$1@dont-email.me> <20260102091518.226@kylheku.com> <10j96mn$jrsp$1@dont-email.me>

Show all headers | View raw


Michael Sanders <porkchop@invalid.foo> writes:
> On Fri, 2 Jan 2026 17:48:16 -0000 (UTC), Kaz Kylheku wrote:
>> On 2026-01-02, Michael Sanders <porkchop@invalid.foo> wrote:
>>> B: because every function must have a return type
>>>    *including function pointers*?
>> 
>> What it is you think type is, in the context of C?
>> 
>> Does type survive into run-time?
>> 
>> If a function pointer is missing type information about return type, and
>> that function pointer is needed for expressing a function call, where
>> does the compiler get the type from?
>
> Its void that's throwing me Kaz. I'm not sure what to think when
> it comes to void pointers.

There were no void pointers in the original code.  There were function
pointers to functions that return void.

> A void pointer (the pointer itself) is not of the type
> it points to no? Its typeless & unknown at compile time

No pointer is of the type it points to.  `int` and `int*` are distinct
types.  (The latter is "derived" from the former.)

> A 'normal' pointer in C is a variable that stores the address
> of another variable.

Yes, if by "pointer" you mean "pointer object".  The standard also uses
the word "pointer" for a *value* of pointer type, for example the result
of a call to malloc().

(The word "variable" is tricky, and the standard rarely uses it.
"Object" is less ambiguous.)

>                      And *is of the type it points to*...

No.

> char str[] = "learning publicly is a humbling experience.";
> char *ptr = str;

And now you're introducing array-to-pointer conversion, a can of
worms that I suggest needn't be opened at this time.  A simpler
example:

    char c = 'l';
    char *ptr = &c;

> Then void enters stage left...
>
> void *genericPointer;
>
> It can point to an integer, a character, a structure, or any other type.
>
> void *genericPointer = &intValue;

"intObject" would be a better name.  An object (variable you like)
is not a value; it holds a value.

> void *genericPointer = str[];

That's a syntax error, but it's ok if you drop the "[]".

void* is a distinct type.  Unlike (most) other object pointer types, it
can't be dereferenced, because it doesn't specify a directly usable type
for the object it points to.

Given:

    int n = 42;
    int  *ip = &n;
    void *vp = &n;

ip points to n.  vp, in a sense, does not, but converting vp
from void* to int* yields a pointer value that does point to n.
A void* holds something you can think of as a raw untyped address.
It doesn't point to anything, but converting it to foo* gives you
a pointer to an object of type foo.

> And it seems to survive a lack of type at compile time.
> Man I'm confused on this. I mean I get void is flexible,
> but 'void' is not void in any sense... its like a mask in some
> way (I'm groping for a definition) I need to study void more.

"void" is an incomplete type that cannot be completed.
"void*" is a pointer type with some special characteristics.

First, a value of any object pointer type can be converted to void* and
back again, yielding the original pointer value.  This is not guaranteed
for (most) other pointer types.  For example, converting a char* value
to int* can lose information.  (On most real-world systems, all object
pointers, and often all function pointers as well, have the same
representation, but the standard doesn't guarantee this.)

Second, conversions between void* and other object pointer types are
often performed implicitly.  You can have an object pointer on one
side of an assignment and a void* pointer on the other side.  On some
(exotic) systems, this conversion might do something more than just
copying the representation.

The character pointer types char*, signed char*, and unsigned char*
share the first characteristic, but not the second.  (In very early
(pre-ANSI) C, char* was commonly used as a generic pointer type before
void and void* were introduced.)

Every pointer value, object, or expression has a well defined type,
specifically a pointer type.  That type specifies the type of
the object it points to -- unless the pointer is of type void*.
(Or it can point to a function; object pointers and function pointers
are distinct.)

The comp.lang.c FAQ <https://www.c-faq.com/> has some good information,
though some of it is a bit dated.  Section 4 covers pointers, and
section 6 covers arrays and pointers.

-- 
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