Casting Pointers

COTTRELL, JAMES cottrell at NBS-VMS.ARPA
Thu Oct 10 11:13:07 AEST 1985


/*
> Ah yes.  Casting pointers.  A favorite passtime for anyone ...

I myself would rather cast aspersions, spells, or plays.

> who uses an architecture where char *  has different format from int *

I don't know. Who does? Maybe someday they'll get a *real* computer.

> > All versions of "lint" that I know of complain when you say something like
> >
> >       (int *)(char *)0;

Hey, what we need is a `(lint *)' type! This will cast any expression
to a type that neither lint nor some guys will complain about.
 
> > since you're casting a pointer of one type to a pointer of another type
> > which is, in general, dangerous.

So is an architexure where all pointers are not the same size as ints.
Even if I were to kneel at the sacred alter of portability, there are all
those other jokers out there that don't know the difference. I propose
passing all parameters on the stack as H-Format Floating Point (that's
VAXish for all you heathens out there), a one size fits all approach.
(add an entire line of :-)'s to this paragraph except for the first line.)
 
> But-but-but... "casting a pointer of one type to a pointer of another
> type" is "in general, dangerous"?  You mean that constructs like
> 
>     foo_ptr = (foo_type *)malloc( sizeof( foo_type ) );
> 
> are "in general, dangerous"?    Say it ain't so!

Not fair. This is a *specific* example. Malloc is special.
 
> I'm not quite sure what Guy was asserting above, but I suspect that it
> all boils down to ...

... is RTFM. How many times do we have to have this discussion? Guy,
maybe you should write a book & make some real money on this stuff.
I'll even buy a copy of it myself.

	jim		cottrell at nbs
*/
------



More information about the Comp.lang.c mailing list