[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Tinycc-devel] compare quoted strings
From: |
KHMan |
Subject: |
Re: [Tinycc-devel] compare quoted strings |
Date: |
Mon, 08 Nov 2010 20:47:01 +0800 |
User-agent: |
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.7) Gecko/20100713 Thunderbird/3.1.1 |
On 11/8/2010 8:44 PM, Christian Jullien wrote:
In fact both gcc and tcc are correct because your snippet uses an undefined
behavior.
"a"=="a" compares the address of two strings which are the address of the
first character, not the character itself as you said.
Now, which one is correct? None or both.
A litteral string is supposed to be constant as if declared:
const char *s="a";
Because litteral strings are constant, a compiler is allowed to share
different strings and define only one instance.
Gcc shares common strings and this "a"=="a" compares the addresses of the
same object. It returns 1.
The OP can get the widely-available C standard draft #1256 and
look at:
Annex J
J.1 Unspecified Behavior:
...
- Whether two string literals result in distinct arrays (6.4.5).
...
Section 6.4.5 states the same thing. The section on initialization
for literal strings also warns about a related kind of unspecified
behaviour.
--
Cheers,
Kein-Hong Man (esq.)
Kuala Lumpur, Malaysia