Can a simple a==b 'hang' in and endless loop?

Dave Hansen iddw at
Wed Jan 18 16:06:44 EST 2006

On 18 Jan 2006 08:41:00 -0800 in comp.lang.python, bonono at

>Fuzzyman wrote:
>> I'm not familiar with the C basic datatypes - I assume it has an array
>> or list like object.
>> Would it contain a sequence of poitners to the members ? In which case
>> they would only be equal if the pointers are the same.
>> In this case :
>> a = ['some string']
>> b = ['somestring']
>> a == b
>> False (probably)
>> Incorrectly using Python syntax for a C example of course :-)
>That depends, the C syntax is like this :
>char *a="hello";
>char *b="hello";
>// true, the compiler knows the two hello are the same and assign the
>same address(sort of id() in python) to a and b

No. The C standard says the compiler is _allowed_ to re-use character
literal constants, but is not _required_ to do so.  So it may be true,
or maybe not, depedning ont he compiler (and, probably, the options
used in its invocation).

>But the following is not
>char *a="hello";
>char *b="_hello";
>char *c=b+1;
>assert(a==c); //false, even the content they point to are the same

Ditto.  The compiler is allowed to re-use the end of "_hello" to
implement "hello", in which case a == b+1, so a==c.

Just to confuse matter slightly, if you change the declarations to
something like

   char a[] = "hello";
   char b[] = "_hello";
   char c[] = "hello";

then a, b, and c are three different strings in three different
locations.  Always.  In this case, the user is allowed to write code

   a[0] = 'j';

to change the first string to "jello" without affecting the other

The essential difference is that in the first two cases, you're
declaring _pointers_ to char, and initializing them to point to string
constants, while in the second case, you're declaring _arrays_ of
char, and setting their initial value using a special initialization

>However, string in the above are not basic types of C and if you want
>to compare the value(like comparing integer), you need to use function
>like strcmp() which again compare byte by byte in the above example and
>give true.

Yes, indeed.


Change is inevitable, progress is not.

More information about the Python-list mailing list