Re: calculating length of an substring
- From: "brasilino@xxxxxxxxx" <brasilino@xxxxxxxxx>
- Date: Fri, 29 Feb 2008 12:45:24 -0800 (PST)
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...@xxxxxxxxx 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.
.
- Follow-Ups:
- Re: calculating length of an substring - TPA
- From: Default User
- Re: calculating length of an substring - TPA
- References:
- calculating length of an substring
- From: brasilino@xxxxxxxxx
- Re: calculating length of an substring
- From: Martin Ambuhl
- 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 - TPA
- Index(es):
Relevant Pages
|