pointer arithmetic

Discussion in 'C++' started by jois.de.vivre@gmail.com, Aug 16, 2005.

  1. Guest

    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?
     
    , Aug 16, 2005
    #1
    1. Advertising

  2. 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
     
    Victor Bazarov, Aug 16, 2005
    #2
    1. Advertising

  3. Guest

    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.
     
    , Aug 16, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. a

    pointer arithmetic

    a, Sep 11, 2003, in forum: C++
    Replies:
    6
    Views:
    527
  2. Marc Schellens

    Iterator/pointer arithmetic

    Marc Schellens, Dec 5, 2003, in forum: C++
    Replies:
    15
    Views:
    853
    tom_usenet
    Dec 8, 2003
  3. dan

    Pointer arithmetic

    dan, Jan 6, 2004, in forum: C++
    Replies:
    1
    Views:
    367
    Jeff Schwab
    Jan 6, 2004
  4. ceo
    Replies:
    8
    Views:
    362
    Pete Becker
    Mar 10, 2005
  5. joshc
    Replies:
    5
    Views:
    575
    Keith Thompson
    Mar 31, 2005
Loading...

Share This Page