0

Two small questions about void* in C:

  1. if a function has void* as an argument, for example:
int foo(void* arg1)

can it receives any pointer type? or should I cast the pointer that I send to the function to void*?

  1. if a function has void* as its return value, for example:
void* bar()

can it returns any pointer type? or should I cast the pointers that I return from the function to void*?

Gabi_Ma
  • 31
  • 3
  • 1
    See https://eli.thegreenplace.net/2009/11/16/void-and-casts-in-c-and-c – jarmod Aug 27 '20 at 16:51
  • 1
    It's not that you can pass/return any pointer type, but you're returning a `void *`, and any pointer to an object can be implicitly converted to a `void *`. You generally shouldn't explicitly cast, because it's not needed and adds unnecessary complexity. – Thomas Jager Aug 27 '20 at 16:51
  • Why not just turn on warnings, don't cast, and see whether the compiler says you needed to – underscore_d Aug 27 '20 at 16:59
  • @underscore_d: Turning on warnings tells you what the compiler you are using at the moment does not like in this specific situation, but it does not fully inform you what the rules of the language are. You might not always learn whether a complaint is the compiler informing you of a bad practice versus the compiler informing you of a language rule violation. You also might not learn in what circumstances the particular code construct is a violation and which it is not. Asking questions about the rules in general is good. – Eric Postpischil Aug 27 '20 at 17:01
  • 1
    "Can `int foo(void* arg1)` receive any pointer type?" **No, it can receive exclusevily pointers to void** however any pointer type is converted (implicitly if needed) before the actual function is called. "Can `void *bar()` return any pointer type?" **No, it returns excluvsively pointers to void** however the calling code converts (implicitly) the pointer received to the correct type. – pmg Aug 27 '20 at 17:04
  • @EricPostpischil Very fair point. I feel like this must be a duplicate though. Not sure if I found the best one on a quick search however. – underscore_d Aug 27 '20 at 17:06

1 Answers1

2

C will automatically convert void * to any pointer to object type and will automatically convert any unqualified pointer to object type to void *, so you do not need to add casts and generally should not. (C++ is different in this regard.)

The fact that such conversions will be performed automatically does not mean the resulting values will be correct for any purpose. If the pointer is being converted back to a type of object it originally pointed to, that is fine. For example, if you pass a pointer to the first struct foo of an array to the bsearch routine and convert the pointer it returns back to struct foo *, that is fine. If you convert it to int *, that is a problem.

(In addition to converting a pointer back to its original type, you can convert any pointer to object type to char * or unsigned char *. This is allowed for the special purpose of manipulating the individual bytes that represent objects and should be avoided in most code.)

Note the above says “pointer to object type.” Pointers to functions are different and are not automatically converted back and forth between void * and should not be converted to pointers to object types except in very special-purpose code (perhaps special situations in operating systems, when loading programs, dynamic linking, and so on).

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312