Article View: comp.std.c
Article #4719Two questions - Pointer equality and ...
From: Michael J. Slifker
Date: Tue, 30 Jan 1996 19:06
Date: Tue, 30 Jan 1996 19:06
53 lines
2016 bytes
2016 bytes
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?
(Oversimplifying what I want to do: I want to define a hash function
on structures containing pointers, and after I determine that two
structures hash to the same index, I need to see if they are identical,
which requires checking that the component pointers are identical.
I've programmed this, and it works (gcc on a DEC Alpha), but I'd
like to clear up my confusion...)
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?
--------------------------------------------------------
Michael Slifker E-mail: M_Slifker@fccc.edu
Senior Programmer/Analyst
Dept. of Biostatistics
Fox-Chase Cancer Center
--------------------------------------------------------
--
--------------------------------------------------------
Michael Slifker E-mail: M_Slifker@fccc.edu
Senior Programmer/Analyst
Dept. of Biostatistics
Technical Details
Message-ID:
<4elq8a$j0b@taurus.fccc.edu>Path:
arc1-ca.pugleaf.net!taurus.fccc.edu!slifkerShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!taurus.fccc.edu!slifker From: slifker@castor.fccc.edu (Michael J. Slifker) Newsgroups: comp.std.c Subject: Two questions - Pointer equality and ... Date: 30 Jan 1996 19:06:50 GMT Organization: Fox Chase Cancer Center, Philadelphia, PA Message-ID: <4elq8a$j0b@taurus.fccc.edu> NNTP-Posting-Host: castor.rm.fccc.edu