(int *)(char *)expr
Guy Harris
guy at sun.uucp
Wed Oct 9 18:36:50 AEST 1985
> > All versions of "lint" that I know of complain when you say something like
> >
> > (int *)(char *)0;
>
> My handy-dandy vanilla-flavored SysV lint blesses expressions like
> these, for example:
"lint" will complain about a possible pointer alignment problem, at least.
> But-but-but... "casting a pointer of one type to a pointer of another
> type" is "in general, dangerous"?
Code which so casts pointers runs the risk of circumventing the type system
of C; as such, there's no guaranteeing *what* it does. It should be
possible to do so - I believe even Ada has unchecked conversions to do so -
but it should not be easy to do so. I think getting a warning, unless the
conversion is to or from "void *", is sufficient. In pre-X3J11 C compilers
- meaning most if not all present C compilers - there is no "void *", so
allowing a cast to override the warnings you get from PCC about illegal
pointer combinations is somewhat excusable. (But if casts are treated as
ways of telling the compiler "trust me, I know what I'm doing", why does
int i;
long l;
i = (int)(l % 2);
still produce "long assignment may lose accuracy" messages? This renders
the "-a" flag less useful, since there's no way to selectively turn off that
particular check only in particular (sub-)expressions. One of the major
reasons why people refuse to use "lint" is that there is a popular
conception that it natters at you about non-problems coupled with the belief
that the real problems it *will* find for you aren't major enough to justify
the aggravation.)
Guy Harris
More information about the Comp.lang.c
mailing list