Illegal string comparisons working?

This is a discussion on Illegal string comparisons working? within the C Programming forums, part of the General Programming Boards category; OK, so I haven't programmed in C for years and years. Haven't had a reason to until now, I'm helping ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Location
    Sweden
    Posts
    41

    Illegal string comparisons working?

    OK, so I haven't programmed in C for years and years. Haven't had a reason to until now, I'm helping out a friend with his first programming course in university - he's new to programming in general, and the first language they use is C. (Don't ask me why.)

    Anyway, while browsing through his work, I thought I found a bug in his code. It's in the vein of:
    Code:
    if (stringvariable == "value") {
    ...which I've always thought was a big no-no (you'd just be comparing pointers, i.e memory addresses), and that you should use strcmp() in string.h for what you intend to do. But just to remind myself and double-check, I wrote the following test program and compiled it with GCC:

    Code:
    #include <stdio.h>
    int main()
    {
        char* p = "abc";
        char* s = "abc";
        (p == s) ? printf("true") : printf("false");
        return 0;
    }
    And hey, it printed "true". Huh. But what about:
    Code:
     
    #include <stdio.h>
    int main()
    {
        char* p = "abc";
        (p == "abc") ? printf("true") : printf("false");
        return 0;
    }
    And what do you know, that also printed "true".

    So has C changed since I last learned about it, or is this just evidence of a damn smart and helpful (at least in this situation) compiler?

  2. #2
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeċ, Sweden
    Posts
    555
    It's an optimization where equal string literals gets treated as the same instead of giving each of them their own memory space.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    It's just the compiler. Many compilers, when you use the same string literal several times in your source code, will make only one section of memory and point all references to that string literal to that location. This makes sense, because it saves memory; and string literals are supposed to be const values, that is, read-only.

    So what happens is you set p to the string "abc", and compare it with the same pointer. If you tried something like this, you'd see that your code only works because you're using string literals:
    Code:
    #include <stdio.h>
    int main()
    {
        char p[BUFSIZ];
        scanf("&#37;s", p);
        (p == "abc") ? printf("true") : printf("false");
        return 0;
    }
    Anyway, relying on string literals being in the same location in memory is not a good idea. It's not portable.

    [edit] Erg, too slow. [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Location
    Sweden
    Posts
    41
    Excellent. I thank you for sharing your wisdom on the subject (and the fast replies)!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 04:48 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  3. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  5. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 07:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21