4 ms·
> This means that it cannot be used safely in portable incantations of variadic functions that expect pointer arguments. Why only variadic functions? The answe
by klingonopera 7y ago
> This means that it cannot be used safely in portable incantations of variadic functions that expect pointer arguments.
Why only variadic functions? The answer to that may make my next question obsolete, namely: Wouldn't only C++ complain about this? In C, isn't the input to anything coerced to the data type it will represent, in actual complete disregard of the input type?
- _kst_ 7y agoFor non-variadic functions, the argument is implicitly converted to the parameter type (if possible). For variadic functions, the compiler doesn't know the parameter type. So to print a null pointer, you need to do this: printf("NULL = %p\n", (void*)NULL); or printf("null pointer = %p\n", (void*)0); The %p format requires an argument of type void*, so you have to convert to that type if necessary.
- colonwqbang 7y agoIn a fixadic function the integer literal would be promoted to a pointer, because the caller knows that the argument should be a pointer. In a variadic function call, the caller guesses the type of each argument based on what is passed. A variadic function needs to know exactly the size of its arguments and sizeof(int) != sizeof(pointer) on many systems. This works: void f(char *p); f(0); /* integer literal auto promoted to pointer */ This probably doesn't, at least not as the number of arguments to f is increased: void f(...); f(0); /* assume type of function is void f(int) */
- deleted 7y ago[deleted]
- loeg 7y agocolonwqbang hit the nail on the head. For non-variadic functions, the function declaration provides the correct parameter type, and caller arguments are coerced to the correct type. > In C, isn't the input to anything coerced to the data type it will represent, in actual complete disregard of the input type? In short: no. Implicit casts between incompatible types are warnings/errors.