int a[10]; int* p=(int*)((&a)+1); But why p isn't equal to ((&a)+1)?

Discussion in 'C++' started by aling, Oct 19, 2005.

  1. aling

    aling Guest

    Given following code snippets:

    int a[10];
    int* p=(int*)((&a)+1);

    when I debuged the code using IDE like VC7.1, I found that:
    a = 0x0012fc50 (int [10])
    p = 0x0012fc78 (int *)
    (&a)+1 = 0x0012fc51 (char *)
    (&a)+2 = 0x0012fc52 (char *)
    (&a)+3 = 0x0012fc53 (char *)

    Why p isn't equal to (&a)+1 ? Why "(&a)+1" became "char*" type?
    aling, Oct 19, 2005
    #1
    1. Advertising

  2. aling

    Rolf Magnus Guest

    aling wrote:

    > Given following code snippets:
    >
    > int a[10];
    > int* p=(int*)((&a)+1);


    Ok, so you take the pointer to the array, add one to it, so it points after
    the array. Then you convert that pointer into a pointer to int. Why?

    > when I debuged the code using IDE like VC7.1, I found that:
    > a = 0x0012fc50 (int [10])
    > p = 0x0012fc78 (int *)
    > (&a)+1 = 0x0012fc51 (char *)
    > (&a)+2 = 0x0012fc52 (char *)
    > (&a)+3 = 0x0012fc53 (char *)
    >
    > Why p isn't equal to (&a)+1 ? Why "(&a)+1" became "char*" type?


    It shouldn't be of type char*. What makes you believe that?
    &a is a pointer to array[10] of int, and (&a)+1 should be of the same type.
    Rolf Magnus, Oct 19, 2005
    #2
    1. Advertising

  3. aling

    John Carson Guest

    "aling" <> wrote in message
    news:
    > Given following code snippets:
    >
    > int a[10];
    > int* p=(int*)((&a)+1);
    >
    > when I debuged the code using IDE like VC7.1, I found that:
    > a = 0x0012fc50 (int [10])
    > p = 0x0012fc78 (int *)
    > (&a)+1 = 0x0012fc51 (char *)
    > (&a)+2 = 0x0012fc52 (char *)
    > (&a)+3 = 0x0012fc53 (char *)
    >
    > Why p isn't equal to (&a)+1 ? Why "(&a)+1" became "char*" type?


    I take it that you have typed (&a)+1 into the Watch window of the debugger.
    The debugger's ability to display values is not perfect (it doesn't always
    understand data types that you enter) and what you are seeing is purely a
    debugger limitation. For what it is worth, a pre-release version of VC++ 8
    shows (&a)+1 correctly and equal to p.


    --
    John Carson
    John Carson, Oct 19, 2005
    #3
  4. aling

    Jim Langston Guest

    "aling" <> wrote in message
    news:...
    > Given following code snippets:
    >
    > int a[10];
    > int* p=(int*)((&a)+1);
    >
    > when I debuged the code using IDE like VC7.1, I found that:
    > a = 0x0012fc50 (int [10])
    > p = 0x0012fc78 (int *)
    > (&a)+1 = 0x0012fc51 (char *)
    > (&a)+2 = 0x0012fc52 (char *)
    > (&a)+3 = 0x0012fc53 (char *)
    >
    > Why p isn't equal to (&a)+1 ? Why "(&a)+1" became "char*" type?


    Because of (&a)+1 is doing pointer array. And a is not an int, but an array
    of 10 ints. So (&a)+1 would point to the next (theoretical) array of 10
    ints which would be sizeof(int)*10 away.

    Try
    int* p=(int*)(&a[0]+1);
    and you should get what you expect, because &a[0] is pointing to an int now.
    Jim Langston, Oct 19, 2005
    #4
  5. aling

    aling Guest

    Yes, I'm using the Watch Window of VC7.1 debugger. And the "char*" type
    of "(&a)+1" is showed by the Watch Window. Now that VC8 has fixed this
    bug, I'm expecting the formal release of VC8, :)
    aling, Oct 19, 2005
    #5
  6. aling

    Greg Guest

    Jim Langston wrote:
    > "aling" <> wrote in message
    > news:...
    > > Given following code snippets:
    > >
    > > int a[10];
    > > int* p=(int*)((&a)+1);
    > >
    > > when I debuged the code using IDE like VC7.1, I found that:
    > > a = 0x0012fc50 (int [10])
    > > p = 0x0012fc78 (int *)
    > > (&a)+1 = 0x0012fc51 (char *)
    > > (&a)+2 = 0x0012fc52 (char *)
    > > (&a)+3 = 0x0012fc53 (char *)
    > >
    > > Why p isn't equal to (&a)+1 ? Why "(&a)+1" became "char*" type?

    >
    > Because of (&a)+1 is doing pointer array. And a is not an int, but an array
    > of 10 ints. So (&a)+1 would point to the next (theoretical) array of 10
    > ints which would be sizeof(int)*10 away.
    >
    > Try
    > int* p=(int*)(&a[0]+1);
    > and you should get what you expect, because &a[0] is pointing to an int now.


    I would suggest further streamlining:

    int *p = &a[1];

    Greg
    Greg, Oct 19, 2005
    #6
  7. Greg wrote:

    >
    > I would suggest further streamlining:
    >
    > int *p = &a[1];
    >


    Are we talking about character count here?

    int *p = a+1;

    --
    Sebastian Redl
    Sebastian Redl, Oct 19, 2005
    #7
  8. aling

    Greg Comeau Guest

    In article <435668fe$0$8024$>,
    Sebastian Redl <> wrote:
    >Greg wrote:
    >> I would suggest further streamlining:
    >>
    >> int *p = &a[1];

    >
    >Are we talking about character count here?
    >
    > int *p = a+1;


    I must be loosing the context, but you just said what he said.
    --
    Greg Comeau / Celebrating 20 years of Comeauity!
    Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
    World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
    Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
    Greg Comeau, Oct 19, 2005
    #8
  9. aling

    Jim Langston Guest

    "Jim Langston" <> wrote in message
    news:O3q5f.21837$...
    >
    > "aling" <> wrote in message
    > news:...
    >> Given following code snippets:
    >>
    >> int a[10];
    >> int* p=(int*)((&a)+1);
    >>
    >> when I debuged the code using IDE like VC7.1, I found that:
    >> a = 0x0012fc50 (int [10])
    >> p = 0x0012fc78 (int *)
    >> (&a)+1 = 0x0012fc51 (char *)
    >> (&a)+2 = 0x0012fc52 (char *)
    >> (&a)+3 = 0x0012fc53 (char *)
    >>
    >> Why p isn't equal to (&a)+1 ? Why "(&a)+1" became "char*" type?

    >
    > Because of (&a)+1 is doing pointer array. And a is not an int, but an
    > array of 10 ints. So (&a)+1 would point to the next (theoretical) array
    > of 10 ints which would be sizeof(int)*10 away.
    >
    > Try
    > int* p=(int*)(&a[0]+1);
    > and you should get what you expect, because &a[0] is pointing to an int
    > now.


    I may be wrong in this.
    Jim Langston, Oct 20, 2005
    #9
    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. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    855
    Mark Rae
    Dec 21, 2006
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,754
    Smokey Grindel
    Dec 2, 2006
  3. lovecreatesbeauty

    Why (type*)pointer isn't equal to *(type**)pointer?

    lovecreatesbeauty, Jan 14, 2006, in forum: C Programming
    Replies:
    10
    Views:
    482
    Peter Shaggy Haywood
    Jan 17, 2006
  4. Replies:
    9
    Views:
    417
    James Kanze
    Apr 17, 2007
  5. PerlFAQ Server
    Replies:
    0
    Views:
    577
    PerlFAQ Server
    Feb 11, 2011
Loading...

Share This Page