calculating length of an substring

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • brasilino@yahoo.com

    calculating length of an substring

    Hi Folks:

    I'm trying to calculating a substring length directly from pointer
    address, like this:

    char *e = NULL, *s = NULL;
    int len = 0;

    s = strchr (url,'.');
    e = strrchr (url,'?');
    len = (int) s - e;

    Since 's' and 'e' are pointers, I think that's can be make a math
    over it, like 's++'.

    Using gcc 4.1.2 (on glibc 2.6 and Linux 2.6.23) it points the
    following error:

    "error: invalid operands to binary -"

    So, how can I calculate this length in a fancy way? I don't want to
    loop through it.

    thanks a lot in advance
    Lucas Brasilino
  • William Pursell

    #2
    Re: calculating length of an substring

    On Feb 29, 6:43 pm, "brasil...@yaho o.com" <brasil...@yaho o.comwrote:
    I'm trying to calculating a substring length directly from pointer
    address, like this:
    >
    char *e = NULL, *s = NULL;
    int len = 0;
    >
    s = strchr (url,'.');
    e = strrchr (url,'?');
    len = (int) s - e;
    You need to check that both s and e are notnull, and you probably
    mean:

    len = (int) (e - s);

    Your version is equivalent to:
    len = ((int)s) - e;

    and the compiler is complaining about subtracting a pointer
    from an int.

    Comment

    • Micah Cowan

      #3
      Re: calculating length of an substring

      "brasilino@yaho o.com" <brasilino@yaho o.comwrites:
      Hi Folks:
      >
      I'm trying to calculating a substring length directly from pointer
      address, like this:
      >
      char *e = NULL, *s = NULL;
      int len = 0;
      >
      s = strchr (url,'.');
      e = strrchr (url,'?');
      len = (int) s - e;
      You wanted
      len = (int)(s - e);
      .. The way you have it, only s will be cast to an int; and you can't
      subtract a pointer from an int!

      The cast is completely superfluous, though; I'd recommend removing
      it. If you like, you can first check that it will actually _fit_ into
      an int (the type of the difference of pointers is ptrdiff_t, defined
      in <stddef.h>. Or better yet, use a ptrdiff_t to store the result (if
      that suits your needs otherwise).

      A size_t actually makes the most sense for representing a string
      length, though, at least to me.

      --
      Micah J. Cowan
      Programmer, musician, typesetting enthusiast, gamer...

      Comment

      • burton.samogradNO@SP4M.gmail.com

        #4
        Re: calculating length of an substring

        "brasilino@yaho o.com" <brasilino@yaho o.comwrites:
        I'm trying to calculating a substring length directly from pointer
        address, like this:
        >
        char *e = NULL, *s = NULL;
        int len = 0;
        >
        s = strchr (url,'.');
        e = strrchr (url,'?');
        len = (int) s - e;
        try using braces:

        len = (int) (s - e);

        casting binds tighter than subtraction.

        --
        burton



        Comment

        • Martin Ambuhl

          #5
          Re: calculating length of an substring

          brasilino@yahoo .com wrote:
          Hi Folks:
          >
          I'm trying to calculating a substring length directly from pointer
          address, like this:
          >
          char *e = NULL, *s = NULL;
          int len = 0;
          >
          s = strchr (url,'.');
          e = strrchr (url,'?');
          len = (int) s - e;
          ^^^^^
          Not only is a cast unneeded, it introduces an error into your program.
          You are trying to subtract a pointer from an int.
          >
          Since 's' and 'e' are pointers, I think that's can be make a math
          over it, like 's++'.
          But your subtraction is a pointer from an int, not a pointer from a
          pointer. If you _must_ use an unneccesary cast, use
          len = (int)(s - e);
          but that means exactly the same thing as the cast-less
          len = s - e;
          >
          Using gcc 4.1.2 (on glibc 2.6 and Linux 2.6.23) it points the
          following error:
          >
          "error: invalid operands to binary -"
          And that is obviously true.

          Comment

          • brasilino@yahoo.com

            #6
            Re: calculating length of an substring

            Hi All!

            I'd like to thanks to everybody who answer. I have forgotten
            the cast precedence from any math operation!!!

            thanks a lot!

            Lucas Brasilino
            brasil...@yahoo .com wrote:
            Hi Folks:
            >
            I'm trying to calculating a substring length directly from pointer
            address, like this:
            >
            char *e = NULL, *s = NULL;
            int len = 0;
            >
            s = strchr (url,'.');
            e = strrchr (url,'?');
            len = (int) s - e;
            >
            ^^^^^
            Not only is a cast unneeded, it introduces an error into your program.
            You are trying to subtract a pointer from an int.
            >
            >
            >
            Since 's' and 'e' are pointers, I think that's can be make a math
            over it, like 's++'.
            >
            But your subtraction is a pointer from an int, not a pointer from a
            pointer. If you _must_ use an unneccesary cast, use
            len = (int)(s - e);
            but that means exactly the same thing as the cast-less
            len = s - e;
            >
            >
            >
            Using gcc 4.1.2 (on glibc 2.6 and Linux 2.6.23) it points the
            following error:
            >
            "error: invalid operands to binary -"
            >
            And that is obviously true.

            Comment

            • Default User

              #7
              Re: calculating length of an substring - TPA

              brasilino@yahoo .com wrote:
              Hi All!
              Please don't top-post. Your replies belong following or interspersed
              with properly trimmed quotes. See the majority of other posts in the
              newsgroup, or:
              <http://www.caliburn.nl/topposting.html >

              Comment

              • William Pursell

                #8
                Re: calculating length of an substring

                On Feb 29, 7:06 pm, Martin Ambuhl <mamb...@earthl ink.netwrote:
                >
                But your subtraction is a pointer from an int, not a pointer from a
                pointer. If you _must_ use an unneccesary cast, use
                len = (int)(s - e);
                but that means exactly the same thing as the cast-less
                len = s - e;

                s - e is of type ptrdiff_t, not int.

                Comment

                • Peter Nilsson

                  #9
                  Re: calculating length of an substring

                  William Pursell <bill.purs...@g mail.comwrote:
                  Martin Ambuhl <mamb...@earthl ink.netwrote:
                  But your subtraction is a pointer from an int, not a
                  pointer from a pointer.  If you _must_ use an
                  unneccesary cast, use
                      len = (int)(s - e);
                  but that means exactly the same thing as the cast-less
                      len = s - e;
                  >
                  s - e is of type ptrdiff_t, not int.
                  Note that len was declared as an int in the original post,
                  in which case there is an implicit conversion to int anyway.

                  --
                  Peter

                  Comment

                  Working...