Thread: strings again

  1. #1
    Registered User larry's Avatar
    Join Date
    Sep 2001
    Posts
    96

    Unhappy strings again

    Code:
    char *number = "0";
    bool iszero1 = ("0" == number);
    bool iszero2 = (0x30 == number[0]);
    bool iszero3 = (NULL == number[1]);
    Why iszero1 == false as iszero2 == true and iszero3 == true too?

    "0" is an array with two char (1 byte each) values, the first is 48 (0x30) and the second is 0 (NULL), isn't it?
    Please excuse my poor english...

  2. #2
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    The first comparison is illegal because for char arrays you need to use strcmp. The other two compare single characters.

    Actually I'd try to dereference the pointer rather than try to compare an address to a string literal, but I still don't see how it would work. You need strcmp.
    Last edited by Troll_King; 10-14-2001 at 09:52 AM.

  3. #3
    Registered User larry's Avatar
    Join Date
    Sep 2001
    Posts
    96
    If you dereference string pointer, you get single char value, don't you? Because char *text is pointer to first item in a string array. Nevermind, strcmp should do what I need. Thanx.
    Please excuse my poor english...

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    412

    Re: strings again

    Originally posted by larry
    Code:
    char *number = "0";
    bool iszero1 = ("0" == number);
    bool iszero2 = (0x30 == number[0]);
    bool iszero3 = (NULL == number[1]);
    Why iszero1 == false as iszero2 == true and iszero3 == true too?

    "0" is an array with two char (1 byte each) values, the first is 48 (0x30) and the second is 0 (NULL), isn't it?
    OK, the reason this happens is because each string literal is added to the data section, and two strings that are the same are both added.

    This instruction:

    bool iszero1 = ("0" == number);

    means "does number point at the address of this literal string?", NOT "does the string pointed at by number the same as this literal string"

    This code is analogous, in what it is doing, to this:

    int * x1, x2;
    x1 = new int;
    x2 = new int;
    *x1 = *x2 = 7;

    bool iszero1 = (x1 == x2); // this is false, because, although they contain the same value, they do not point to the same address!

    strcmp is the string equivalent of doing this:

    bool iszero2 = (*x1 == *x2);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM