string comparison

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • junky_fellow@yahoo.co.in

    #1

    string comparison

    Consider the following piece of code:
    char *str = "Hello";
    if (str = "Hello")
    printf("\nstrin g 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 ?

  • Krishanu Debnath

    #2
    Re: string comparison


    junky_fellow@ya hoo.co.in wrote:[color=blue]
    > Consider the following piece of code:
    > char *str = "Hello";
    > if (str = "Hello")
    > printf("\nstrin g 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 ?[/color]

    '=' is assignment operator.

    Krishanu

    Comment

    • Sensei

      #3
      Re: string comparison

      On 2005-08-09 12:38:58 +0200, junky_fellow@ya hoo.co.in said:
      [color=blue]
      > Consider the following piece of code:
      > char *str = "Hello";
      > if (str = "Hello")
      > printf("\nstrin g 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 ?[/color]


      strcmp


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

      --
      Sensei <senseiwa@tin.i t>

      cd /pub
      more beer

      Comment

      • junky_fellow@yahoo.co.in

        #4
        Re: string comparison


        Sensei wrote:[color=blue]
        > On 2005-08-09 12:38:58 +0200, junky_fellow@ya hoo.co.in said:
        >[color=green]
        > > Consider the following piece of code:
        > > char *str = "Hello";
        > > if (str = "Hello")
        > > printf("\nstrin g 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 ?[/color]
        >
        >
        > strcmp
        >
        >
        > Check the string.h header file for all the functions.
        >
        > --
        > Sensei <senseiwa@tin.i t>
        >
        > cd /pub
        > more beer[/color]

        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.

        Comment

        • Stephane Zuckerman

          #5
          Re: string comparison

          > I know that there's a C library function "strcmp" for string[color=blue]
          > 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.[/color]

          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)

          Comment

          • Krishanu Debnath

            #6
            Re: string comparison


            junky_fellow@ya hoo.co.in wrote:[color=blue]
            > Sensei wrote:[color=green]
            > > On 2005-08-09 12:38:58 +0200, junky_fellow@ya hoo.co.in said:
            > >[color=darkred]
            > > > Consider the following piece of code:
            > > > char *str = "Hello";
            > > > if (str = "Hello")
            > > > printf("\nstrin g 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 ?[/color]
            > >
            > >
            > > strcmp
            > >
            > >
            > > Check the string.h header file for all the functions.
            > >
            > > --
            > > Sensei <senseiwa@tin.i t>
            > >
            > > cd /pub
            > > more beer[/color]
            >
            > 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.[/color]

            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

            Comment

            • junky_fellow@yahoo.co.in

              #7
              Re: string comparison


              Krishanu Debnath wrote:[color=blue]
              > junky_fellow@ya hoo.co.in wrote:[color=green]
              > > Sensei wrote:[color=darkred]
              > > > On 2005-08-09 12:38:58 +0200, junky_fellow@ya hoo.co.in said:
              > > >
              > > > > Consider the following piece of code:
              > > > > char *str = "Hello";
              > > > > if (str = "Hello")
              > > > > printf("\nstrin g 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 <senseiwa@tin.i t>
              > > >
              > > > cd /pub
              > > > more beer[/color]
              > >
              > > 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.[/color]
              >
              > 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[/color]

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

              Comment

              • junky_fellow@yahoo.co.in

                #8
                Re: string comparison


                Krishanu Debnath wrote:[color=blue]
                > junky_fellow@ya hoo.co.in wrote:[color=green]
                > > Consider the following piece of code:
                > > char *str = "Hello";
                > > if (str = "Hello")
                > > printf("\nstrin g 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 ?[/color]
                >
                > '=' is assignment operator.
                >
                > Krishanu[/color]
                I am sorry, I meant (str == "Hello")

                Comment

                • Krishanu Debnath

                  #9
                  Re: string comparison


                  junky_fellow@ya hoo.co.in wrote:[color=blue]
                  > Krishanu Debnath wrote:[color=green]
                  > > junky_fellow@ya hoo.co.in wrote:[color=darkred]
                  > > > Sensei wrote:
                  > > > > On 2005-08-09 12:38:58 +0200, junky_fellow@ya hoo.co.in said:
                  > > > >[/color][/color][/color]

                  <snip>
                  [color=blue][color=green]
                  > >
                  > > 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[/color]
                  >
                  > Can you please specify which section of C standard specify this
                  > conversion ?[/color]

                  c99 draft Section 6.3.2.1p3

                  --
                  krishanu@india. ti.com

                  Comment

                  • kernelxu@hotmail.com

                    #10
                    Re: string comparison

                    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("\nstri ng matches\n");" will be excuted .

                    Comment

                    • Chen Ming

                      #11
                      Re: string comparison

                      u should use *lint* to check your program.

                      <junky_fellow@y ahoo.co.in>
                      ??????:11235864 07.988940.29044 0@z14g2000cwz.g ooglegroups.com ...[color=blue]
                      >
                      > Sensei wrote:[color=green]
                      >> On 2005-08-09 12:38:58 +0200, junky_fellow@ya hoo.co.in said:
                      >>[color=darkred]
                      >> > Consider the following piece of code:
                      >> > char *str = "Hello";
                      >> > if (str = "Hello")
                      >> > printf("\nstrin g 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 ?[/color]
                      >>
                      >>
                      >> strcmp
                      >>
                      >>
                      >> Check the string.h header file for all the functions.
                      >>
                      >> --
                      >> Sensei <senseiwa@tin.i t>
                      >>
                      >> cd /pub
                      >> more beer[/color]
                      >
                      > 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.
                      >[/color]


                      Comment

                      • Default User

                        #12
                        Re: string comparison

                        junky_fellow@ya hoo.co.in wrote:
                        [color=blue]
                        > Consider the following piece of code:[/color]


                        Consider reading through the FAQ.




                        Brian

                        Comment

                        • pete

                          #13
                          Re: string comparison

                          Krishanu Debnath wrote:
                          [color=blue]
                          > "Hello" has type 'array 5 of char'.[/color]

                          (sizeof "Hello" == 6)

                          --
                          pete

                          Comment

                          • pete

                            #14
                            Re: string comparison

                            junky_fellow@ya hoo.co.in wrote:
                            [color=blue]
                            > What I wanted to ask is that the comparison
                            > if (str == "Hello") doesn't give any compile time error.[/color]

                            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

                            Comment

                            • bwaichu@yahoo.com

                              #15
                              Re: string comparison

                              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

                              Comment

                              Working...