Re: calculating length of an substring
- From: Micah Cowan <micah@xxxxxxxxxx>
- Date: Fri, 29 Feb 2008 19:03:29 GMT
"brasilino@xxxxxxxxx" <brasilino@xxxxxxxxx> 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/
.
- References:
- calculating length of an substring
- From: brasilino@xxxxxxxxx
- calculating length of an substring
- Prev by Date: Re: Is there stack associated when a executing an inline function?
- Next by Date: Re: Is there stack associated when a executing an inline function?
- Previous by thread: Re: calculating length of an substring
- Next by thread: Re: calculating length of an substring
- Index(es):
Relevant Pages
|