Velocity Reviews > calculating length of an substring

# calculating length of an substring

brasilino@yahoo.com
Guest
Posts: n/a

 02-29-2008
Hi Folks:

I'm trying to calculating a substring length directly from pointer

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.

Lucas Brasilino

William Pursell
Guest
Posts: n/a

 02-29-2008
On Feb 29, 6:43 pm, "(E-Mail Removed)" <(E-Mail Removed)> wrote:

> I'm trying to calculating a substring length directly from pointer
>
> 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);

len = ((int)s) - e;

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

Micah Cowan
Guest
Posts: n/a

 02-29-2008
"(E-Mail Removed)" <(E-Mail Removed)> writes:

> Hi Folks:
>
> I'm trying to calculating a substring length directly from pointer
>
> 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

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/

Guest
Posts: n/a

 02-29-2008
"(E-Mail Removed)" <(E-Mail Removed)> writes:

> I'm trying to calculating a substring length directly from pointer
>
> 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

Martin Ambuhl
Guest
Posts: n/a

 02-29-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hi Folks:
>
> I'm trying to calculating a substring length directly from pointer
>
> 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.

brasilino@yahoo.com
Guest
Posts: n/a

 02-29-2008
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

> (E-Mail Removed) wrote:
> > Hi Folks:

>
> > I'm trying to calculating a substring length directly from pointer

>
> > 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.

Default User
Guest
Posts: n/a

 02-29-2008
(E-Mail Removed) wrote:

> Hi All!

with properly trimmed quotes. See the majority of other posts in the
newsgroup, or:
<http://www.caliburn.nl/topposting.html>

William Pursell
Guest
Posts: n/a

 02-29-2008
On Feb 29, 7:06 pm, Martin Ambuhl <(E-Mail Removed)> 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.

Peter Nilsson
Guest
Posts: n/a

 02-29-2008
William Pursell <(E-Mail Removed)> wrote:
> Martin Ambuhl <(E-Mail Removed)> 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

pete
Guest
Posts: n/a

 03-01-2008
Peter Nilsson wrote:
>
> William Pursell <(E-Mail Removed)> wrote:
> > Martin Ambuhl <(E-Mail Removed)> 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