Suspicious pointer conversion warning in Turbo C 2.0
Chris Torek
chris at mimsy.umd.edu
Thu Jul 26 16:04:03 AEST 1990
In article <1990Jul25.230836.2442 at Octopus.COM> stever at Octopus.COM
(Steve Resnick) writes:
>As far as I know, when I provide a prototype for a function which accepts
>a void pointer, the compiler should not bitch about type conversions between
>pointers. The code fragment below generates a warning message ....
The ANSI standard only says when diagnostics are required; it makes no
prohibitions about extra diagnostics. In other words, compilers are free
to warn about anything they like (`foo.c, line 354: warning: code written
at 4 AM').
Of course, some warnings are rather less useful than others (although the
above might well be particularly good, actually).
>The parameter bitched about is the second pointer.
>The first pointer in both cases is not complained about.
[everything but the relevant bits trimmed:]
>void AddLNode(void ** Hptr, void * Nptr);
> Tlist *Head, *Walker;
> AddLNode(&Head,Walker); /* This statement generates a warning */
Now this is exactly backwards: a good compiler should complain about
the first parameter, not the second. Argument passing is semantically
equivalent to assignment, so the commented line is rather like writing:
void **Hptr;
void *Nptr;
Hptr = &Head;
Nptr = Walker;
The first assignment expands to
<object, pointer to pointer to void, Hptr> =
&<object, pointer to Tlist, Head>;
which becomes
<object, void **, Hptr> = <value, Tlist **, &Head>;
which is an attempt to stuff a `Tlist **' into a `void **'. The C
standard guarantees that any pointer type (e.g., Tlist **) must fit
into a `void *'; it makes no such guarantee about a `void **'. In
other words, this need not work, and the compiler ought to complain.
The second assignment expands to
<object, pointer to void, Nptr> =
&<object, pointer to Tlist, Walker>;
which becomes
<object, void *, Nptr> = <value, Tlist **, Walker>;
which (because Tlist** fits in void*) has to `work' (the compiler is
free to complain, but this should at least be an option, or customers
will turn away in droves).
--
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain: chris at cs.umd.edu Path: uunet!mimsy!chris
More information about the Comp.lang.c
mailing list