calculating length of an substring

Discussion in 'C Programming' started by brasilino@yahoo.com, Feb 29, 2008.

  1. Guest

    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
    , Feb 29, 2008
    #1
    1. Advertising

  2. On Feb 29, 6:43 pm, "" <> wrote:

    > 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.
    William Pursell, Feb 29, 2008
    #2
    1. Advertising

  3. Micah Cowan Guest

    "" <> writes:

    > 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...
    http://micah.cowan.name/
    Micah Cowan, Feb 29, 2008
    #3
  4. Guest

    "" <> writes:

    > 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
    , Feb 29, 2008
    #4
  5. 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.
    Martin Ambuhl, Feb 29, 2008
    #5
  6. Guest

    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

    > 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.
    , Feb 29, 2008
    #6
  7. Default User Guest

    Re: calculating length of an substring - TPA

    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>
    Default User, Feb 29, 2008
    #7
  8. On Feb 29, 7:06 pm, Martin Ambuhl <> wrote:

    >
    > 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.
    William Pursell, Feb 29, 2008
    #8
  9. William Pursell <> wrote:
    > Martin Ambuhl <> wrote:
    > > 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
    Peter Nilsson, Feb 29, 2008
    #9
  10. pete Guest

    Peter Nilsson wrote:
    >
    > William Pursell <> wrote:
    > > Martin Ambuhl <> wrote:
    > > > 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.


    N869

    6.5.16.1 Simple assignment

    Constraints
    [#1] One of the following shall hold:
    -- the left operand has qualified or unqualified
    arithmetic type and the right has arithmetic type;

    Semantics
    [#2] In simple assignment (=), the value of the right
    operand is converted to the type of the assignment
    expression and replaces the value stored in the object
    designated by the left operand.

    --
    pete
    pete, Mar 1, 2008
    #10
    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. Mitchua
    Replies:
    5
    Views:
    2,738
    Eric J. Roode
    Jul 17, 2003
  2. Xah Lee
    Replies:
    1
    Views:
    388
    Jürgen Exner
    Jan 11, 2005
  3. Replies:
    5
    Views:
    452
  4. Xah Lee
    Replies:
    2
    Views:
    85
    Peter Wyzl
    Jan 12, 2005
  5. Replies:
    3
    Views:
    200
    Sherm Pendley
    Aug 3, 2005
Loading...

Share This Page