Article View: comp.std.c
Article #1765Re: Casting pointers
From: Henry Spencer
Date: Mon, 01 Oct 1990 18:14
Date: Mon, 01 Oct 1990 18:14
17 lines
1106 bytes
1106 bytes
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
Technical Details
Message-ID:
<1990Oct1.181406.23127@zoo.toronto.edu>Path:
arc1-ca.pugleaf.net!utzoo!henryReferences:
<1990Sep29.201144.23113@maths.tcd.ie>Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c Path: arc1-ca.pugleaf.net!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