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

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

aling
Guest
Posts: n/a

 10-19-2005
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?

Rolf Magnus
Guest
Posts: n/a

 10-19-2005
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.

John Carson
Guest
Posts: n/a

 10-19-2005
"aling" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com
> 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

Jim Langston
Guest
Posts: n/a

 10-19-2005

"aling" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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.

aling
Guest
Posts: n/a

 10-19-2005
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,

Greg
Guest
Posts: n/a

 10-19-2005

Jim Langston wrote:
> "aling" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
> > 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

Sebastian Redl
Guest
Posts: n/a

 10-19-2005
Greg wrote:

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

Are we talking about character count here?

int *p = a+1;

--
Sebastian Redl

Greg Comeau
Guest
Posts: n/a

 10-19-2005
In article <435668fe\$0\$8024\$(E-Mail Removed)>,
Sebastian Redl <(E-Mail Removed)> 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?

Jim Langston
Guest
Posts: n/a

 10-20-2005

"Jim Langston" <(E-Mail Removed)> wrote in message
news:O3q5f.21837\$(E-Mail Removed)...
>
> "aling" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
>> 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.