J
Jens Thoms Toerring
Hi,
there's a very recent thread about 0 being an integer constant
and a null pointer constant at the same time (with or without
context). Not being a language lawyer (yet;-) and having some
difficulties following some of the arguments I decided to start
a new thread instead of chiming in on the other one.
My question is: is it necessary to cast 0 to '(void *) 0' when
used as one of the arguments to a variadic function? I have
seen a number of rather convincing arguments for and against
the necessity to do so and the other thread finally got me
sufficently confused to ask...
So, if you have a variadic function called e.g. like this
va_func( "%d%p%d", -1, 0, -1 );
where the "format string" tells the function (like for printf())
to expect an int, a void pointer and then again an int, is this
guaranteed to always work correctly?
The thing I am concerned about is the third argument, '0'. I do
not see yet how the compiler can deduce from the context (or even
without context?) that a (NULL) pointer is meant here. And if the
size of a pointer and that of an int differs (or if the processor
has dedicated data and address registers that might be used for
passing the arguments to the function), how can it figure out what
(or how) exactly to pass to the function? Is the compiler actually
supposed to produce code that can't go wrong or would one have to
write
va_func( "%d%p%d", -1, NULL, -1 );
to make sure there can't be any problems? And are there any
significant differences between C89 and C99 in this respect?
Best regards, Jens
there's a very recent thread about 0 being an integer constant
and a null pointer constant at the same time (with or without
context). Not being a language lawyer (yet;-) and having some
difficulties following some of the arguments I decided to start
a new thread instead of chiming in on the other one.
My question is: is it necessary to cast 0 to '(void *) 0' when
used as one of the arguments to a variadic function? I have
seen a number of rather convincing arguments for and against
the necessity to do so and the other thread finally got me
sufficently confused to ask...
So, if you have a variadic function called e.g. like this
va_func( "%d%p%d", -1, 0, -1 );
where the "format string" tells the function (like for printf())
to expect an int, a void pointer and then again an int, is this
guaranteed to always work correctly?
The thing I am concerned about is the third argument, '0'. I do
not see yet how the compiler can deduce from the context (or even
without context?) that a (NULL) pointer is meant here. And if the
size of a pointer and that of an int differs (or if the processor
has dedicated data and address registers that might be used for
passing the arguments to the function), how can it figure out what
(or how) exactly to pass to the function? Is the compiler actually
supposed to produce code that can't go wrong or would one have to
write
va_func( "%d%p%d", -1, NULL, -1 );
to make sure there can't be any problems? And are there any
significant differences between C89 and C99 in this respect?
Best regards, Jens