Newsgroups: comp.std.c Path: utzoo!henry From: henry@zoo.toronto.edu (Henry Spencer) Subject: Re: Casting pointers Message-ID: <1990Oct1.181406.23127@zoo.toronto.edu> Organization: U of Toronto Zoology References: <1990Sep29.201144.23113@maths.tcd.ie> Date: Mon, 1 Oct 90 18:14:06 GMT In article <1990Sep29.201144.23113@maths.tcd.ie> tim@maths.tcd.ie (Timothy Murphy) writes: >char *buf = "abc"; > int *n = (int*) buf; > printf("%d", *n); >Should this always work? >On the Mac, with THINK C, >it only works if buf has an even address. ... >Is that a bug, according to standard C? What you are doing is not portable, and your machine is quite entitled to object to it. Converting pointer-to-more-aligned-type to pointer-to-less- aligned type (e.g. `int *' to `char *') is guaranteed to produce a usable pointer, but not vice-versa. You are working in the Twilight Zone of undefined behavior, and all bets are off. -- Imagine life with OS/360 the standard | Henry Spencer at U of Toronto Zoology operating system. Now think about X. | henry@zoo.toronto.edu utzoo!henry