string comparison

Discussion in 'C Programming' started by junky_fellow@yahoo.co.in, Aug 9, 2005.

  1. Guest

    Consider the following piece of code:
    char *str = "Hello";
    if (str = "Hello")
    printf("\nstring matches\n");

    str is pointer to char and "Hello" is a string literal whose
    type is "array of char".

    How can we compare two different objects for equality ?
    Is some conversion is being done before that comparison ?
    If yes, which conversion rule from C standard is applied here ?
    , Aug 9, 2005
    #1
    1. Advertising

  2. Sensei Guest

    On 2005-08-09 12:38:58 +0200, said:

    > Consider the following piece of code:
    > char *str = "Hello";
    > if (str = "Hello")
    > printf("\nstring matches\n");
    >
    > str is pointer to char and "Hello" is a string literal whose
    > type is "array of char".
    >
    > How can we compare two different objects for equality ?
    > Is some conversion is being done before that comparison ?
    > If yes, which conversion rule from C standard is applied here ?



    strcmp


    Check the string.h header file for all the functions.

    --
    Sensei <>

    cd /pub
    more beer
    Sensei, Aug 9, 2005
    #2
    1. Advertising

  3. wrote:
    > Consider the following piece of code:
    > char *str = "Hello";
    > if (str = "Hello")
    > printf("\nstring matches\n");
    >
    > str is pointer to char and "Hello" is a string literal whose
    > type is "array of char".
    >
    > How can we compare two different objects for equality ?
    > Is some conversion is being done before that comparison ?
    > If yes, which conversion rule from C standard is applied here ?


    '=' is assignment operator.

    Krishanu
    Krishanu Debnath, Aug 9, 2005
    #3
  4. Guest

    Sensei wrote:
    > On 2005-08-09 12:38:58 +0200, said:
    >
    > > Consider the following piece of code:
    > > char *str = "Hello";
    > > if (str = "Hello")
    > > printf("\nstring matches\n");
    > >
    > > str is pointer to char and "Hello" is a string literal whose
    > > type is "array of char".
    > >
    > > How can we compare two different objects for equality ?
    > > Is some conversion is being done before that comparison ?
    > > If yes, which conversion rule from C standard is applied here ?

    >
    >
    > strcmp
    >
    >
    > Check the string.h header file for all the functions.
    >
    > --
    > Sensei <>
    >
    > cd /pub
    > more beer


    I know that there's a C library function "strcmp" for string
    comparison. What I wanted to ask is that the comparison
    if (str == "Hello") doesn't give any compile time error.
    Nor does it give any Warning. That means some conversion
    should have been done because we cannot compare two objects
    of different types.
    , Aug 9, 2005
    #4
  5. > I know that there's a C library function "strcmp" for string
    > comparison. What I wanted to ask is that the comparison
    > if (str == "Hello") doesn't give any compile time error.
    > Nor does it give any Warning. That means some conversion
    > should have been done because we cannot compare two objects
    > of different types.


    When you write
    if (a_var == "a string") { /* ... */ }
    you're really writing "is the value of a_var equals to the value of the
    address that points to the constant string 'a string' ?" So, yes, there is
    a conversion, but maybe not the one you thought...


    --
    "Je deteste les ordinateurs : ils font toujours ce que je dis, jamais ce
    que je veux !"
    "The obvious mathematical breakthrough would be development of an easy
    way to factor large prime numbers." (Bill Gates, The Road Ahead)
    Stephane Zuckerman, Aug 9, 2005
    #5
  6. wrote:
    > Sensei wrote:
    > > On 2005-08-09 12:38:58 +0200, said:
    > >
    > > > Consider the following piece of code:
    > > > char *str = "Hello";
    > > > if (str = "Hello")
    > > > printf("\nstring matches\n");
    > > >
    > > > str is pointer to char and "Hello" is a string literal whose
    > > > type is "array of char".
    > > >
    > > > How can we compare two different objects for equality ?
    > > > Is some conversion is being done before that comparison ?
    > > > If yes, which conversion rule from C standard is applied here ?

    > >
    > >
    > > strcmp
    > >
    > >
    > > Check the string.h header file for all the functions.
    > >
    > > --
    > > Sensei <>
    > >
    > > cd /pub
    > > more beer

    >
    > I know that there's a C library function "strcmp" for string
    > comparison. What I wanted to ask is that the comparison
    > if (str == "Hello") doesn't give any compile time error.
    > Nor does it give any Warning. That means some conversion
    > should have been done because we cannot compare two objects
    > of different types.


    Okay, now you have realized that you are talking about equality
    operator. Yes, "Hello" has type 'array 5 of char'. But in value
    context it decays to pointer to the first element of the array
    which has type 'pointer to char'.

    Krishanu
    Krishanu Debnath, Aug 9, 2005
    #6
  7. Guest

    Krishanu Debnath wrote:
    > wrote:
    > > Sensei wrote:
    > > > On 2005-08-09 12:38:58 +0200, said:
    > > >
    > > > > Consider the following piece of code:
    > > > > char *str = "Hello";
    > > > > if (str = "Hello")
    > > > > printf("\nstring matches\n");
    > > > >
    > > > > str is pointer to char and "Hello" is a string literal whose
    > > > > type is "array of char".
    > > > >
    > > > > How can we compare two different objects for equality ?
    > > > > Is some conversion is being done before that comparison ?
    > > > > If yes, which conversion rule from C standard is applied here ?
    > > >
    > > >
    > > > strcmp
    > > >
    > > >
    > > > Check the string.h header file for all the functions.
    > > >
    > > > --
    > > > Sensei <>
    > > >
    > > > cd /pub
    > > > more beer

    > >
    > > I know that there's a C library function "strcmp" for string
    > > comparison. What I wanted to ask is that the comparison
    > > if (str == "Hello") doesn't give any compile time error.
    > > Nor does it give any Warning. That means some conversion
    > > should have been done because we cannot compare two objects
    > > of different types.

    >
    > Okay, now you have realized that you are talking about equality
    > operator. Yes, "Hello" has type 'array 5 of char'. But in value
    > context it decays to pointer to the first element of the array
    > which has type 'pointer to char'.
    >
    > Krishanu


    Can you please specify which section of C standard specify this
    conversion ?
    , Aug 9, 2005
    #7
  8. Guest

    Krishanu Debnath wrote:
    > wrote:
    > > Consider the following piece of code:
    > > char *str = "Hello";
    > > if (str = "Hello")
    > > printf("\nstring matches\n");
    > >
    > > str is pointer to char and "Hello" is a string literal whose
    > > type is "array of char".
    > >
    > > How can we compare two different objects for equality ?
    > > Is some conversion is being done before that comparison ?
    > > If yes, which conversion rule from C standard is applied here ?

    >
    > '=' is assignment operator.
    >
    > Krishanu

    I am sorry, I meant (str == "Hello")
    , Aug 9, 2005
    #8
  9. wrote:
    > Krishanu Debnath wrote:
    > > wrote:
    > > > Sensei wrote:
    > > > > On 2005-08-09 12:38:58 +0200, said:
    > > > >


    <snip>

    > >
    > > Okay, now you have realized that you are talking about equality
    > > operator. Yes, "Hello" has type 'array 5 of char'. But in value
    > > context it decays to pointer to the first element of the array
    > > which has type 'pointer to char'.
    > >
    > > Krishanu

    >
    > Can you please specify which section of C standard specify this
    > conversion ?


    c99 draft Section 6.3.2.1p3

    --
    Krishanu Debnath, Aug 9, 2005
    #9
  10. Guest

    there may not be any warnnings or errors ,even though you had used
    "(str == "Hello") ", they just campare their address value ,not the
    strings.

    if you use (str = "Hello") , the judge expression is always true .
    "printf("\nstring matches\n");" will be excuted .
    , Aug 9, 2005
    #10
  11. Chen Ming Guest

    u should use *lint* to check your program.

    <>
    ??????:...
    >
    > Sensei wrote:
    >> On 2005-08-09 12:38:58 +0200, said:
    >>
    >> > Consider the following piece of code:
    >> > char *str = "Hello";
    >> > if (str = "Hello")
    >> > printf("\nstring matches\n");
    >> >
    >> > str is pointer to char and "Hello" is a string literal whose
    >> > type is "array of char".
    >> >
    >> > How can we compare two different objects for equality ?
    >> > Is some conversion is being done before that comparison ?
    >> > If yes, which conversion rule from C standard is applied here ?

    >>
    >>
    >> strcmp
    >>
    >>
    >> Check the string.h header file for all the functions.
    >>
    >> --
    >> Sensei <>
    >>
    >> cd /pub
    >> more beer

    >
    > I know that there's a C library function "strcmp" for string
    > comparison. What I wanted to ask is that the comparison
    > if (str == "Hello") doesn't give any compile time error.
    > Nor does it give any Warning. That means some conversion
    > should have been done because we cannot compare two objects
    > of different types.
    >
    Chen Ming, Aug 9, 2005
    #11
  12. Default User Guest

    wrote:

    > Consider the following piece of code:



    Consider reading through the FAQ.




    Brian
    Default User, Aug 9, 2005
    #12
  13. pete Guest

    Krishanu Debnath wrote:

    > "Hello" has type 'array 5 of char'.


    (sizeof "Hello" == 6)

    --
    pete
    pete, Aug 10, 2005
    #13
  14. pete Guest

    wrote:

    > What I wanted to ask is that the comparison
    > if (str == "Hello") doesn't give any compile time error.


    After you get that settled, you need to know that
    ("Hello" == "Hello")
    isn't guaranteed true.
    Identical string literals may either refer to the same object
    or to different objects.

    --
    pete
    pete, Aug 10, 2005
    #14
  15. Guest

    You are confused.

    char *string; /* string is a pointer */

    char *string = "hello"; /* string is a pointer that points to 'h' */

    char *string; /* declare the pointer */
    string = malloc(5); /* sets up memory to point to*/

    string = "hello"; /* assigns "hello" to memory pointed to */

    Here's a small C program:

    #include <stdio.h>
    #include <stdlib.h>

    int
    main() {

    char *string;
    string = malloc(5);

    string = "hello\n";

    (void)printf("%s\n", string);

    exit(0);
    }

    Here's it disassembled:

    ..section .rodata # read only data

    ..LC0:
    .string "hello\n"

    LC1:
    .string "%s\n"

    ..section .text #program starts here

    ..global main

    main:
    pushq %rbp # set up the stack
    movq %rsp, %rbp
    subq $16, %rsp # allocate space on the stack for
    # 16 bytes
    movl $5, %edi # pass the size to malloc
    call malloc
    movq %rax, -8(%rbp) # %rax is the string variable
    movq $.LC0, -8(%rbp) # this is the memory assign
    movq -8(%rbp), %rsi # "hello\n"
    movl $.LC1, %edi # "%s\n"
    movl $0, %eax # NOP padding
    call printf
    movl $0, %edi # exit(0)
    call exit

    So now you want to compare two strings. If you look at libc strcmp:

    int
    strcmp(const char *s1, const char *s2)
    {
    while (*s1 == *s2++)
    if (*s1++ == 0)
    return (0);
    return (*(unsigned char *)s1 - *(unsigned char *)--s2);
    }

    What is being looked at are the individual values of each element of
    string. Since 'A' is really 0x41, when checking for comparison, you
    are really comparing two values and doing simple subtraction.

    C hides this from you. The assembly would use cmps or else you could
    just manually do the compare.

    Brian
    , Aug 10, 2005
    #15
  16. On 9 Aug 2005 03:38:58 -0700, wrote:

    >Consider the following piece of code:
    >char *str = "Hello";
    >if (str = "Hello")
    > printf("\nstring matches\n");
    >
    >str is pointer to char and "Hello" is a string literal whose
    >type is "array of char".
    >
    >How can we compare two different objects for equality ?
    >Is some conversion is being done before that comparison ?
    >If yes, which conversion rule from C standard is applied here ?


    After fixing the = to == in the if, the answer to your question is you
    don't know if they are different objects. The compiler is allowed to
    reuse string literals. It is entirely possible that str points to the
    same string that is used in the if. It is also possible that str
    points to a different string, even though the two strings contain same
    six characters.

    Just for fun, you might try
    if ("Hello" == "Hello") ...

    The same implementation defined behavior allows the expression to
    evaluate to 1 or 0 as a result of how the compiler implements
    duplicate string literals.


    <<Remove the del for email>>
    Barry Schwarz, Aug 10, 2005
    #16
  17. "" <> writes:
    > char *string; /* string is a pointer */
    >
    > char *string = "hello"; /* string is a pointer that points to 'h' */
    >
    > char *string; /* declare the pointer */
    > string = malloc(5); /* sets up memory to point to*/


    You've allocated 5 bytes (assuming the malloc succeeds), which isn't
    enough room for "hello"; you need 6 bytes to allow for the trailing
    '\0'.

    > string = "hello"; /* assigns "hello" to memory pointed to */


    No, that's a pointer assignment. The string literal refers to a
    statically allocated block of 6 bytes with the value "hello\0". The
    assignment causes string to point to that block. If this follows the
    malloc above, you've just created a memory leak.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Aug 10, 2005
    #17
  18. pete wrote:
    > Krishanu Debnath wrote:
    >
    > > "Hello" has type 'array 5 of char'.

    >
    > (sizeof "Hello" == 6)
    >
    > --
    > pete


    Thanks for the correction.

    Krishanu
    Krishanu Debnath, Aug 10, 2005
    #18
  19. Guest

    C strings have trailing zeros, but those are only important if you wish
    to count out the length of the string or perform other string functions
    after the assignment. You do not need to have the trailing zero.

    My example would have been better if I used write() instead of
    printf(). The trailing zero is just a C concept. The only purpose the
    zero serves is to tell you where the end of the string is.

    I can code a string that starts with it's size and does not terminate
    with a zero. I think Pascal does strings that way.

    The allocation of memory occurs either in .bss or in the heap.
    The address of the string sits on the stack. You can write strings to
    the stack in C, but I cannot imagine why you would want to do that.

    What I do not understand is why you think I created a memory leak.
    Unless I use another function that will search for the terminating
    zero, I do not see the problem. For example, strcmp will search for
    the terminating zero.

    Brian
    , Aug 10, 2005
    #19
  20. "" <> writes:
    > C strings have trailing zeros, but those are only important if you wish
    > to count out the length of the string or perform other string functions
    > after the assignment. You do not need to have the trailing zero.


    They're only important if you want to use C strings, which are the
    most common use of character arrays in C.

    > My example would have been better if I used write() instead of
    > printf(). The trailing zero is just a C concept. The only purpose the
    > zero serves is to tell you where the end of the string is.


    C concepts are what we discuss here.

    [snip]

    > What I do not understand is why you think I created a memory leak.
    > Unless I use another function that will search for the terminating
    > zero, I do not see the problem. For example, strcmp will search for
    > the terminating zero.


    Please provide some context; don't assume that everyone can see the
    article to which you're replying. I'm sick and tired of explaining
    how to do this, so just search for "google broken reply link" in this
    newsgroup.

    Here's a fragment of the code you posted earlier:

    char *string;
    string = malloc(5);

    string = "hello\n";

    The first statement allocates a block of 5 bytes (assuming the
    malloc() call succeeds) and causes string to point to the beginning of
    the newly allocated block.

    The second statement causes string to point to the beginning of the
    string literal "hello\n" (which occupies 7 bytes). You've now lost
    your only pointer to the memory allocated by malloc(), and you have no
    way to release it (there's nothing you can pass to free()). That's a
    memory leak.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Aug 10, 2005
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jason
    Replies:
    2
    Views:
    27,800
    Phil Hanna
    Sep 20, 2003
  2. grz02

    Java String comparison

    grz02, Feb 23, 2005, in forum: Java
    Replies:
    40
    Views:
    15,780
    nooobody
    Mar 5, 2005
  3. Replies:
    21
    Views:
    1,362
    Alex Vinokur
    Aug 18, 2007
  4. Smithers
    Replies:
    12
    Views:
    1,150
    Ben Voigt [C++ MVP]
    Jul 7, 2009
  5. Deepu
    Replies:
    1
    Views:
    213
    ccc31807
    Feb 7, 2011
Loading...

Share This Page