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

A

aling

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?
 
R

Rolf Magnus

aling said:
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.
 
J

John Carson

aling said:
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.
 
J

Jim Langston

aling said:
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.
 
A

aling

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, :)
 
G

Greg

Jim said:
aling said:
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
 
J

Jim Langston

Jim Langston said:
aling said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top