calculating length of an substring

B

brasilino

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
 
W

William Pursell

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

Micah Cowan

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

burton.samogradNO

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

Martin Ambuhl

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

brasilino

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
 
W

William Pursell

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

Peter Nilsson

William Pursell said:
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.
 
P

pete

Peter said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top