Article View: comp.lang.c
Article #246Re: compare literals / if( "cat"== "dog") {}
From: Keith Thompson
Date: Sun, 11 Oct 2026 22:47
Date: Sun, 11 Oct 2026 22:47
26 lines
2223 bytes
2223 bytes
MitchAlsup <user5857@newsgrouper.org.invalid> writes:
> fir <profesor.fir@gmail.com> posted:
> -------------------
>>
>> if("cat" == "cat") will return true and
>> if("cat" == "dog") will return false
>
> "cat" in C means a pointer p points at {'c' 'a' 't' 00} in memory; so,
> if you want character comparison you want:
>
> if( strcmp( "cat", "dog" ) == 0 ) //cat = dog
[...]
Not quite. "cat" is of type char[4]. Like any expression of array
type, it is implicitly converted ("adjusted" might be more accurate)
to a pointer to the array object's first element in some but not
all contexts. This is why sizeof "cat" == 4 (regardless of the
size of a pointer), and &"cat" is of type char(*)[4] (pointer to
array of 4 chars), not char**.
But yes, in the context of ("cat" == "dog"), the array-to-pointer
conversion is applied and values of type char* are compared.
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */
Technical Details
Message-ID:
<11ah3lq$329bt$2@kst.eternal-september.org>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!kst.eternal-september.org!.POSTED!not-for-mailReferences:
<11ae962$2365v$1@dont-email.me> <1791743331-5857@newsgrouper.org>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!kst.eternal-september.org!.POSTED!not-for-mail
From: Keith Thompson <Keith.S.Thompson+u@gmail.com>
Newsgroups: comp.lang.c
Subject: Re: compare literals / if( "cat"== "dog") {}
Date: Sun, 11 Oct 2026 15:47:22 -0700
Organization: None to speak of
Lines: 26
Message-ID: <11ah3lq$329bt$2@kst.eternal-september.org>
References: <11ae962$2365v$1@dont-email.me> <1791743331-5857@newsgrouper.org>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Date: Sun, 11 Oct 2026 22:47:22 +0000 (UTC)
Injection-Info: kst.eternal-september.org;
logging-data="3220861"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+ANs+FGu8efrx525AgHsLJ"; posting-host="50698cca2518b68a86663845e71c144a"
User-Agent: Gnus/5.13 (Gnus v5.13)
Cancel-Lock: sha1:oIpNW4fHY3k1uj083nJbDmb1X2s= sha256:EHLWdNLsh/CUF8UntY8VPNlcPgGWImB5tMY7mAndyJg=
sha1:hl1kPsfDcfDxXITix2mcUUykxDI= sha256:6PAXHSbq7/wEEQbi6ayq9My2/ROGUcgeSwzjTiNN6cM=