doubt??

This is a discussion on doubt?? within the C Programming forums, part of the General Programming Boards category; Hi! I have a few doubts regarding string declarations? Why is the output of the following program 'false'? Code: int ...

  1. #1
    Unregistered
    Guest

    doubt??

    Hi! I have a few doubts regarding string declarations?

    Why is the output of the following program 'false'?

    Code:
    int main()
    {
     char str1[]="hello";
     char str2[]="hello";
    
     if(str1==str2)
        printf("true");
     else
        printf("false");
    
    return 0;
    }
    Also what is the differece between the two declarations:
    char str[]="hello"; and char *str="hello";

    thanks a lot

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Lookup how to compare strings with strcmp(). This is wrong
    >if(str1==str2)

    >char str[]="hello";
    Is an array holding the word hello and a null terminator.

    >char *str="hello";
    Is a pointer to a string (the word hello and a null terminator).
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    Whilst this always prints false
    Code:
    int main()
    {
      char str1[]="hello";
      char str2[]="hello";
    
      if(str1==str2)
        printf("true");
      else
        printf("false");
    
      return 0;
    }
    This may print true
    Code:
    int main()
    {
      char *str1="hello";
      char *str2="hello";
    
      if(str1==str2)
        printf("true");
      else
        printf("false");
    
      return 0;
    }

  4. #4
    Unregistered
    Guest
    Yes the second code prints "true". Can someone explain me why it is so?


    thanks in advance

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Yes the second code prints "true". Can someone explain me why it is so?
    Because the second one is not comparing the word hello with hello, it is comparing the memory addresses within the pointers. If they both point to the same thing, they will be equal.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Banned borko_b's Avatar
    Join Date
    Jun 2002
    Location
    Well... I live in Bulgaria :)
    Posts
    100
    in C/C++ arrays of characters and strings are not compared like this:
    if(str1 == str2)


    You should compare strings by strcmp() function
    it compares the bytes the string is holding
    (byte by byte) untill it reaches the first nil charachter ('\0')
    from one the strings
    (not NULL ... NULL is not '\0'... it is ((void*)0)
    )
    :
    Code:
    char str1[] = "Hello world";
    char str2[] = "Hello world";
    char str3[] = "Hello world";
    int ret = 0;
    
    ret = strcmp(str1,str2);
    if(!ret) { /*They are equall*/
       printf("\"%s\" == \"%s\"\n",str1,str2);
    }
    else if(ret > 0) { /*str1 is bigger*/
       printf("\"%s\" > \"%s\"\n",str1,str2);
    }
    else { /*str2 is bigger*/
       printf("\"%s\" < \"%s\"\n",str1,str2);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doubt in pointer.
    By shwetha_siddu in forum C Programming
    Replies: 5
    Last Post: 03-21-2009, 01:28 AM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 12:49 PM
  3. Replies: 4
    Last Post: 12-10-2006, 06:08 PM
  4. Doubt abt Storage!
    By kalamram in forum C Programming
    Replies: 1
    Last Post: 04-21-2006, 05:30 AM
  5. Greatest C++ Doubt
    By vasanth in forum C++ Programming
    Replies: 15
    Last Post: 02-28-2002, 03:41 AM

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