Article View: comp.std.c
Article #1823Evaluation of expressions involving string literals
From: Cameron Paine
Date: Thu, 11 Oct 1990 06:48
Date: Thu, 11 Oct 1990 06:48
34 lines
1490 bytes
1490 bytes
I'm porting some code to an ANSI-conforming compiler and have encountered
an anomaly which my reading of K&R(2) and H&S(2) hasn't resolved. In the
original code, I used many expressions (the reason for doing this is not
relevant) such as:
char *cptr;
cptr = "XYhello, world!" + 2;
which has traditionally rendered:
*cptr == 'h' /* because cptr => "hello, world!" */
Under the ANSI-conforming compiler, I find that:
*cptr == 'X' /* because cptr => "XYhello, world!" */
Experimentation has revealed that:
cptr = "XYhello, world!";
cptr += 2;
achieves the result I'm after but this is of little use to me.
The question then is whether the new compiler's behaviour is correct. The
documentation I have implies that it isn't but I'd like to hear what the
lawyers think.
Thanks.
Cameron
--
cbp@foster.avid.oz - ACSnet
cbp@foster.avid.oz.au - Internet
...!{hplabs,mcvax,nttlab,ukc,uunet}!munnari!foster.avid.oz.au!cbp - UUCP
Technical Details
Message-ID:
<1990Oct11.064854.12867@foster.avid.oz>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!samsung!munnari.oz.au!labtam!foster!cbpShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!samsung!munnari.oz.au!labtam!foster!cbp
From: cbp@foster.avid.oz (Cameron Paine)
Newsgroups: comp.std.c
Subject: Evaluation of expressions involving string literals
Summary: What does ("literal" + 2) point to?
Keywords: string literal; expression; pointer arithmetic; broken compiler?
Message-ID: <1990Oct11.064854.12867@foster.avid.oz>
Date: 11 Oct 90 06:48:54 GMT
Organization: Avid Systems Pty Ltd, Melbourne, Australia.
Lines: 34