🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #4720

Re: Two questions - Pointer equality and ...

#4720
From: Tanmoy Bhattacharya
Date: Tue, 30 Jan 1996 22:08
53 lines
2334 bytes
In article <4elq8a$j0b@taurus.fccc.edu> slifker@castor.fccc.edu
(Michael J. Slifker) writes: 
<snip>
   Question 1:

   According to K&R II, "pointer comparison is defined only for parts
   of the same object". In particular, this is stated to apply to 
   equality operators (== and !=). Harbison & Steele (_C: A Reference
   Manual_) agree with this in the case of relational operators, but
   state that pointer equality is defined provided both operands are
   pointers to compatible types. The latter makes much more sense to
   me, but can anyone tell me what the Standard has to say? 

Equality and inequality are defined for any two valid pointers of the
same type, or if one is a pointer to void and the other is a pointer
to an object or incomplete type.

<snip>
   Question 2:

   I've compiled the following program with gcc:

   -----------

   #include <stdio.h>

   char *str1 = "Hello, World\n";
   const char *str2 = "Hello, World\n";

   int main(void)
   {
     str1[0] = 'J';
     printf("%s", str2);
     return 0;
   }

   -----------

   ...and it prints out "Jello, World". This seems odd, at least to
   me. Is this a bug, or is the behavior above officially undefined?

Attempt to modify a string literal results in undefined behaviout. Any
behaviour by the implementation is then valid.

Cheers
Tanmoy
--
tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
<http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
Technical Details
Message-ID: <TANMOY.96Jan30150808@qcd.lanl.gov>
Path: arc1-ca.pugleaf.net!newshost.lanl.gov!tanmoy
References: <4elq8a$j0b@taurus.fccc.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!newshost.lanl.gov!tanmoy
From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
Newsgroups: comp.std.c
Subject: Re: Two questions - Pointer equality and ...
Date: 30 Jan 1996 22:08:08 GMT
Organization: Los Alamos National Laboratory
Message-ID: <TANMOY.96Jan30150808@qcd.lanl.gov>
References: <4elq8a$j0b@taurus.fccc.edu>
NNTP-Posting-Host: qcd.lanl.gov
Mime-Version: 1.0
Content-Type: text
In-reply-to: slifker@castor.fccc.edu's message of 30 Jan 1996 19:06:50 GMT