tinycc-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Tinycc-devel] compare quoted strings


From: Ivo
Subject: Re: [Tinycc-devel] compare quoted strings
Date: Mon, 8 Nov 2010 13:46:31 +0100
User-agent: KMail/1.9.9

On Monday 08 November 2010, 12:35:44, Henry Kroll wrote:
> This little snippet prints "true" if compiled with gcc and "not true" if
> compiled with tcc.
>
> int main (void){
>     printf ("%sok\n","a"=="a"?"":"not ");
>     return 0;
> }
>
> Note that "a"=="a" is not what it looks like. We are comparing the
> address of the pointer "a" with itself. If the book I just looked at
> (Banahan, et al., 1991) interprets the standard correctly, a quoted
> string serves as a pointer to the first element, thus "a"=="a" should
> return true.

This all depends on whether the compiler detects identical strings or not. 
Tcc stores the first and the second string independently in the data 
segment, hence the pointers don't match. Gcc optimizes the duplicate string 
away. It even detects the following:

#include <stdio.h>
char *a = "hello, world\n";
char *b = "world\n";
int main(void){
  printf(a);
  printf(b);
  printf("%s\n", (a+7) == b ? "yes" : "no");
  return 0;
}

output (gcc):
hello, world
world
yes

But there's nothing in the standard that demands (a+7) to be equal to b. It 
is perfectly valid to store both strings separately.

--Ivo



reply via email to

[Prev in Thread] Current Thread [Next in Thread]