# 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. ### alingGuest

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

2. ### Rolf MagnusGuest

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

3. ### John CarsonGuest

"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
4. ### Jim LangstonGuest

"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
5. ### alingGuest

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
6. ### GregGuest

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
7. ### Sebastian RedlGuest

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
8. ### Greg ComeauGuest

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
9. ### Jim LangstonGuest

"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