home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c      Meh, in C you gotta define EVERYTHING      243,242 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 242,710 of 243,242   
   Keith Thompson to Michael Sanders   
   Re: function pointer question   
   02 Jan 26 12:07:02   
   
   From: Keith.S.Thompson+u@gmail.com   
      
   Michael Sanders  writes:   
   > On Fri, 2 Jan 2026 17:48:16 -0000 (UTC), Kaz Kylheku wrote:   
   >> On 2026-01-02, Michael Sanders  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  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 */   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca