473,406 Members | 2,633 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

calculating length of an substring

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 '08 #1
8 1893
On Feb 29, 6:43 pm, "brasil...@yahoo.com" <brasil...@yahoo.comwrote:
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.
Feb 29 '08 #2
"br*******@yahoo.com" <br*******@yahoo.comwrites:
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/
Feb 29 '08 #3
"br*******@yahoo.com" <br*******@yahoo.comwrites:
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 '08 #4
br*******@yahoo.com 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 '08 #5
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...@yahoo.com 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 '08 #6
br*******@yahoo.com 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>
Feb 29 '08 #7
On Feb 29, 7:06 pm, Martin Ambuhl <mamb...@earthlink.netwrote:
>
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.
Feb 29 '08 #8
William Pursell <bill.purs...@gmail.comwrote:
Martin Ambuhl <mamb...@earthlink.netwrote:
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
Feb 29 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Ron Adam | last post by:
Hi, I'm having fun learning Python and want to say thanks to everyone here for a great programming language. Below is my first Python program (not my first program) and I'd apreciate any...
10
by: Howard Martin | last post by:
I have a form at http://www.develop.check.com.au/hh/order.html where an extended sub-total and then a grand total is calculated as soon as a quantity is input by a user. I would like to be able to...
3
by: Sam | last post by:
I want to divide character into 2 section. Example, 200412 divided into 2004 in A block and 12 in B block. Please advise how to use left(string, length) or right(string, length) for above...
2
by: manmit.walia | last post by:
Hello All and Thank You for your time, I am stuck on this exception handleing error. The problem is that when I run my application the application works perfect but sometimes, I get this error. I...
2
by: Gidi | last post by:
Hello, I've a strange problem with substring function. I'm geting a string from my SQL DataBase and i want to split it to two substings. here is what i do: if(temp.Length>80 &&...
6
by: kellygreer1 | last post by:
What is a good one line method for doing a "length safe" String.Substring? The VB classes offer up the old Left function so that string s = Microsoft.VisualBasic.Left("kelly",200) // s will =...
24
by: garyusenet | last post by:
I'm working on a data file and can't find any common delimmiters in the file to indicate the end of one row of data and the start of the next. Rows are not on individual lines but run accross...
2
by: hgarg | last post by:
The onpaint() is getting called before calculating the required parameters by listBox1_SelectedIndexChanged function. I tried calling it at the end of this function. But of no use. Due to this issue...
1
by: cmb3587 | last post by:
My code runs fine for the most part...the only time it fails is when I type in a negative to end the array. I don't want the negative number to be included in the array and I thought that is what...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.