Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

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?

 
Reply With Quote
 
 
 
 
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.

 
Reply With Quote
 
 
 
 
John Carson
Guest
Posts: n/a
 
      10-19-2005
"aling" <> wrote in message
news: 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

 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      10-19-2005

"aling" <> wrote in message
news: 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.


 
Reply With Quote
 
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,

 
Reply With Quote
 
Greg
Guest
Posts: n/a
 
      10-19-2005

Jim Langston wrote:
> "aling" <> wrote in message
> news: 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

 
Reply With Quote
 
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
 
Reply With Quote
 
Greg Comeau
Guest
Posts: n/a
 
      10-19-2005
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?
 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      10-20-2005

"Jim Langston" <> wrote in message
news:O3q5f.21837$...
>
> "aling" <> wrote in message
> news: 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Internet Sharing: Equal upload speeds but un-equal download speeds =?Utf-8?B?TkpU?= Wireless Networking 3 09-15-2007 06:22 AM
why is int a[0] not allowed, but int* a = new int[0] is? haijin.biz@gmail.com C++ 9 04-17-2007 09:01 AM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57