Merging of string literals guaranteed by C std?



Hi group,

I have a question about string literals and the address that they point
to. Does the standard *guarantee* that two identical string literals
actually point to the same address. I.e. can we safely assert:

assert("foo" == "foo");

Or can it maybe only be asserted if the literal occurs in one
compilation unit (i.e. not across compilation units)?

My gut feeling tells me that I cannot rely on the addresses being
identical, but I cannot find it in N1124. It would make things much
easier/cooler if the standard would assert that in my situation, but I
don't want to rely on compiler behavior alone (gcc merges the string
literals into one address even with -O0).

Best regards,
Johannes


--
Wo hattest Du das Beben nochmal GENAU vorhergesagt?
Zumindest nicht öffentlich!
Ah, der neueste und bis heute genialste Streich unsere großen
Kosmologen: Die Geheim-Vorhersage.
- Karl Kaos über Rüdiger Thomas in dsa <hidbv3$om2$1@xxxxxxxxxxxxxxxxx>
.



Relevant Pages

  • Re: Merging of string literals guaranteed by C std?
    ... I have a question about string literals and the address that they point ... Does the standard *guarantee* that two identical string literals ... compilation unit? ...
    (comp.lang.c)
  • Re: ISO SQL Date Format / ISO Date Formats - question to --CELKO-- on your conti
    ... I'm not interested in the output string so long as its standard and consistent. ... I'm absolutely positive that people in the industry accept that because of legacy inconsistency YYYYMMDD is the correct input format to use on systems because it allows a move towards the DATE type but also keeps consistency with legacy systems and doesn't cause $100;sK in resource for unnecessary development and testing. ... The ISO SQL Standard date format is YYYY-MM-DD only which is termed a "date literal". ... String literals on the other hand are subject to regional settings and because its a string literal can accept any ISO standard date format so YYYYMMDD is indeed legal. ...
    (microsoft.public.sqlserver.programming)
  • Re: A question about pointer of array.
    ... Attempting to modify a string literal in C produces undefined behavior ... standard specifically states that the behavior is undefined. ... doesn't refer to string literals as string constants). ... Attempting to modify such an array ...
    (comp.lang.c)
  • Re: int * vs char *
    ... the current standard, then why doesn't the next standard ... specify that string literal have type const char[] instead ... of string literals. ...
    (comp.lang.c)
  • Re: A question about pointer of array.
    ... The attempt to modify the array might succeed, ... Since this is the same thing the standard says about attempting to modify an array of char defined to be 'const', the practical effect is the same, as far as the C standard is concerned. ... Then when you go to page 194 and read A2.6 String Literals, you find the first sentence says: ...
    (comp.lang.c)