K
Keith Thompson
Tim Rentsch said:Thank you for pointing out the obvious and failing to respond
to the question.
My pleasure. }
Sorry, I assumed the question was hypothetical. If you actually need
to worry about pre-ANSI compilers, I'm not sure how much I can help
you (or how much you already know).
There's a tool called "ansi2knr" (google it) that converts, or at
least attempts to convert, ANSI C to K&R C. It doesn't necessarily do
the whole job; I think it deals mainly with function prototypes.
I know K&R C has malloc() returning char* (because it has no void*
type). I don't remember whether it requires a cast when assigning the
result to another pointer type, but I think pre-ANSI compilers tended
to be more permissive about implicit conversions. It also depends on
the compiler; remember, there was no real standard back then.
The obvious brute-force approach is to use "#ifdef __STDC__" and
provide K&R and ANSI versions of any incompatible code. The result is
ugly, but it makes it fairly easy to strip out the K&R stuff if that
requirement finally goes away.
Or you can write pure ANSI code and invoke ansi2knr as part of your
build process. If the result isn't compilable, you might be able to
do some ad-hoc patching, but that might be more effort than it's
worth.
One thing to watch out for is function arguments of types char, short
(and their signed and unsigned variants) and float. In K&R-style
functions, these types are promoted to int or double; in ANSI-style
functions, they're not.
Of course if casting the result of malloc() makes your job easier, go
ahead and do it, perhaps wrapping it in a macro. Just make sure that
<stdlib.h> is included when you compile in ANSI mode.
Your goal, IMHO, should be to move away from the dependency on K&R C
as soon as you can. To state the obvious yet again, it's been 15
years.