Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > pointer arithmetic

Reply
Thread Tools

pointer arithmetic

 
 
jois.de.vivre@gmail.com
Guest
Posts: n/a
 
      08-16-2005
Hello,

I'm trying to get the number of bytes between two pointers of type
char*. For example, say I have the following code:

int main()
{
char* message = "this is a test";
char* p1 = strstr(message, "is");
char* p2 = strstr(message, "test");

//find # characters between p2 and p1

return 0;
}

To find the number of chars between p2 and p1, I tried

int len = (int)p2 - (int)p1;

in the legacy C manner, but it gives me an error:

error: cast from 'char*' to 'int' loses precision

I tried using C++ casts:

int len = reinterpret_cast<int>(p2) - reinterpret_cast<int>(p1);

but it gives me the same error.

I read GNU C++ user's guide and it says that it only permits pointer
arithmetic between pointers to void and pointers to functions. So I
tried the following:

int len = ((void*)p2 - (void*)p1);

Now it gives me the error:

error: invalid use of void

Which i'm guessing is because i'm trying to assign an 'void*' to an
'int' type.

Basically all I want to know how to get the address of a pointer and
store it as an int. Is there any way to do this?

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-16-2005
wrote:
> I'm trying to get the number of bytes between two pointers of type
> char*. For example, say I have the following code:
>
> int main()
> {
> char* message = "this is a test";
> char* p1 = strstr(message, "is");
> char* p2 = strstr(message, "test");
>
> //find # characters between p2 and p1
>
> return 0;
> }
>
> To find the number of chars between p2 and p1, I tried
>
> int len = (int)p2 - (int)p1;
>
> in the legacy C manner, but it gives me an error:
>
> error: cast from 'char*' to 'int' loses precision
>
> I tried using C++ casts:
>
> int len = reinterpret_cast<int>(p2) - reinterpret_cast<int>(p1);
>
> but it gives me the same error.
>
> I read GNU C++ user's guide and it says that it only permits pointer
> arithmetic between pointers to void and pointers to functions. So I
> tried the following:
>
> int len = ((void*)p2 - (void*)p1);
>
> Now it gives me the error:
>
> error: invalid use of void
>
> Which i'm guessing is because i'm trying to assign an 'void*' to an
> 'int' type.
>
> Basically all I want to know how to get the address of a pointer and
> store it as an int. Is there any way to do this?
>


Have you tried simply

ptrdiff_t len = p2 - p1;

???

V
 
Reply With Quote
 
 
 
 
jois.de.vivre@gmail.com
Guest
Posts: n/a
 
      08-16-2005
Victor Bazarov wrote:
> wrote:
> > I'm trying to get the number of bytes between two pointers of type
> > char*. For example, say I have the following code:
> >
> > int main()
> > {
> > char* message = "this is a test";
> > char* p1 = strstr(message, "is");
> > char* p2 = strstr(message, "test");
> >
> > //find # characters between p2 and p1
> >
> > return 0;
> > }
> >
> > To find the number of chars between p2 and p1, I tried
> >
> > int len = (int)p2 - (int)p1;
> >
> > in the legacy C manner, but it gives me an error:
> >
> > error: cast from 'char*' to 'int' loses precision
> >
> > I tried using C++ casts:
> >
> > int len = reinterpret_cast<int>(p2) - reinterpret_cast<int>(p1);
> >
> > but it gives me the same error.
> >
> > I read GNU C++ user's guide and it says that it only permits pointer
> > arithmetic between pointers to void and pointers to functions. So I
> > tried the following:
> >
> > int len = ((void*)p2 - (void*)p1);
> >
> > Now it gives me the error:
> >
> > error: invalid use of void
> >
> > Which i'm guessing is because i'm trying to assign an 'void*' to an
> > 'int' type.
> >
> > Basically all I want to know how to get the address of a pointer and
> > store it as an int. Is there any way to do this?
> >

>
> Have you tried simply
>
> ptrdiff_t len = p2 - p1;
>
> ???
>
> V


Great! Thanks, that works.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointer to pointer or reference to pointer A C++ 7 07-05-2011 07:49 PM
Pointer to pointer Vs References to Pointer bansalvikrant@gmail.com C++ 4 07-02-2009 10:20 AM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
Usual Arithmetic Conversions-arithmetic expressions joshc C Programming 5 03-31-2005 02:23 AM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57