473,791 Members | 3,122 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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?

Oct 19 '05 #1
8 4722
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.

Oct 19 '05 #2
"aling" <li*********@16 3.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.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

Oct 19 '05 #3

"aling" <li*********@16 3.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.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.
Oct 19 '05 #4
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, :)

Oct 19 '05 #5

Jim Langston wrote:
"aling" <li*********@16 3.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.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

Oct 19 '05 #6
Greg wrote:

I would suggest further streamlining:

int *p = &a[1];


Are we talking about character count here?

int *p = a+1;

--
Sebastian Redl
Oct 19 '05 #7
In article <43************ **********@tune ws.univie.ac.at >,
Sebastian Redl <e0******@stude nt.tuwien.ac.at > 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?
Oct 19 '05 #8

"Jim Langston" <ta*******@rock etmail.com> wrote in message
news:O3******** ***********@fe0 3.lga...

"aling" <li*********@16 3.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.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.
Oct 20 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
1869
by: Ben | last post by:
Hi all, I'm not yet good at thinking the right way in c++ so although I could solve this problem, I'm not sure if they way I'm thinking of is the best way to do it. I need a data type or class or something that can hold either an int, or a float, knows which one it is holding, and will allow me to do comparisons with instances of it without the code which asks for the comparison having to know which one it is. So maybe I could do it...
7
2849
by: Robin Haswell | last post by:
Hey guys. This should just be a quickie: I can't figure out how to convert r"\x2019" to an int - could someone give me a hand please? Cheers -Rob
2
4235
by: wudoug119 | last post by:
This is my code and it will take any number that I input and say it is a prime number. Please help me... int Prime(int prime) //declares isPrime as a function using integers { int primeCount = 2; //declares primeCount as an int variable and sets it equal to 2 while(primeCount < prime) //checks if primeCount is less than prime {
7
46399
by: stefan.istrate | last post by:
I have a short int variable and I'm trying to read it with scanf("%d",&x); but I'm getting a warning message which sounds like this: warning: format '%d' expects type 'int*', but argument 2 has type 'short int*' How can I avoid this warning and still read with "scanf"? I mention that I don't want to use "cout". Maybe this problem is for C (not for C ++), but the program is in C++ because i use after this the Standard Template Library.
19
3262
by: Hallvard B Furuseth | last post by:
Do anyone know of an architecture where this can break? T *ptr1, *ptr2; ... if (ptr1 == ptr2) if (CHAR_BIT*sizeof(T*) <= (width of int)) /*otherwise undefined*/ assert((int)ptr1 == (int)ptr2); (Feel free to replace int with another integer type if that helps to break something.)
10
5029
by: arnuld | last post by:
It is quite an ugly hack but it is all I am able to come up with for now :-( and it does the requires work. I want to improve the program, I know you people have much better ideas ;-) /* C++ Primer - 4/e * * exercise 9.20 * STATEMENT: * Write a program to to compare whether a vector<intcontains the
19
3903
by: =?iso-8859-1?b?VG9t4XMg0yBoyWlsaWRoZQ==?= | last post by:
Coming originally from C++, I used to do the likes of the following, using a pointer in a conditional: void Func(int *p) { if (p) { *p++ = 7; *p++ = 8;
8
2979
by: RN1 | last post by:
The book I am referring to learn ASP states the following about the Int & Fix VBScript Maths functions: ========================================= Both Int & Fix return the integer portion of the number but the difference lies in handling negative numbers. Int returns the first integer lesser than or equal to the number whereas Fix returns the first integer greater than or equal to the number. =========================================
0
994
by: DomiNeug | last post by:
Hello, Since a while i have to find a way of comparing "Sets" (multiple int Values) and so to find equal sets. There simply 3 tables ValueList with: ID int ValueListHasValue: ID int, ValueListID int, ValueID int Value: ID int, Description varchar(max) The sets are defined in the ValueList has ValueTable. Any combination and order is possible as there is also no restriction on the amount of Values in a ValueList. My first approach was to...
4
3155
by: damiensawyer | last post by:
Hi, I've recently moved to c# framework 2 and have just discovered the comiler warning "The result of the expression is always 'false' since a value of type 'int' is never equal to 'null' of type 'int?'" Can someone please tell me best practise for determining in properties if an int has been initialised? Thanks very much in advance,
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10419
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10201
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10147
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9987
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5552
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4100
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3709
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2910
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.