I recently find int *p is a GENUINE pointer, which means, p itself is
an integer that takes 4 bytes in memory; however int a[3], where the
array name a is a VIRTUAL pointer, whose physical address is just
itself and could not be retrieved.
I find that int *p creates a *genuine* pointer to an integer, where p
is per se an 4-byte integer stored in memory. The value of p indicates
the address of the integer it points to. Therefore, *p, p, &p should
all be different. So far so good.
However, as is said in many textbook, int a[4] create an array, with
the array name 'a' as a pointer to this array.
Therefore I just speculated that *a,a, &a should also be different
values; I further guessed that creating and initializing a[4] consumes
4*4bytes to hold the value of a[4] and another 4 byte pointer a to
addressing the starting point of the array. But it is not the case in
Visual C++ 2005. &a=a in the result. a itself does not have an
address.
Who can explain this? All comments are welcomed.
#include <iostream>
using namespace std;
void main(){
int a[]={1,2,3,4};
for (int i=0;i<4;i++){
printf("a[%d]:%4d|addr:%6x\n",i,a[i],&(a[i]));
}
printf("\n\n");
printf("&a: \t%8x\n",&a);
printf("a: \t%8x\n",a);
printf("*a:\t%8x\n",*a);
int *p;
p=new int(0xff);
printf("&p:\t%8x\n",&p);
printf("p:\t%8x\n",p);
printf("*p:\t%8x\n",*p);
}
a[0]: 1|addr:12ff64
a[1]: 2|addr:12ff68
a[2]: 3|addr:12ff6c
a[3]: 4|addr:12ff70
&a: 12ff64
a: 12ff64
*a: 1
&p: 12ff60
p: 475fd0
*p: ff 2 1296
Xiaozhong a écrit :
I recently find int *p is a GENUINE pointer, which means, p itself is
an integer that takes 4 bytes in memory; however int a[3], where the
array name a is a VIRTUAL pointer, whose physical address is just
itself and could not be retrieved.
I find that int *p creates a *genuine* pointer to an integer, where p
is per se an 4-byte integer stored in memory. The value of p indicates
the address of the integer it points to. Therefore, *p, p, &p should
all be different. So far so good.
However, as is said in many textbook, int a[4] create an array, with
the array name 'a' as a pointer to this array.
Therefore I just speculated that *a,a, &a should also be different
values; I further guessed that creating and initializing a[4] consumes
4*4bytes to hold the value of a[4] and another 4 byte pointer a to
addressing the starting point of the array. But it is not the case in
Visual C++ 2005. &a=a in the result. a itself does not have an
address.
Who can explain this? All comments are welcomed.
#include <iostream>
using namespace std;
void main(){
int a[]={1,2,3,4};
for (int i=0;i<4;i++){
printf("a[%d]:%4d|addr:%6x\n",i,a[i],&(a[i]));
}
printf("\n\n");
printf("&a: \t%8x\n",&a);
printf("a: \t%8x\n",a);
printf("*a:\t%8x\n",*a);
int *p;
p=new int(0xff);
printf("&p:\t%8x\n",&p);
printf("p:\t%8x\n",p);
printf("*p:\t%8x\n",*p);
}
a[0]: 1|addr:12ff64
a[1]: 2|addr:12ff68
a[2]: 3|addr:12ff6c
a[3]: 4|addr:12ff70
&a: 12ff64
a: 12ff64
*a: 1
&p: 12ff60
p: 475fd0
*p: ff
Take a look at this article: http://www.cplusplus.com/doc/tutorial/pointers.html
Have nice memory diagrams explaining pointers as well as a section
regarding your particular question "Pointers and arrays"
J.
Xiaozhong wrote:
I recently find int *p is a GENUINE pointer, which means, p itself is
an integer that takes 4 bytes in memory;
Just for completeness, on my machine, int* uses 8 bytes and int uses 4,
so pointers and ints are not interchangeable. This is a common mistake,
which I seen often and which I have come across just today in 3rd-party
code.
The rest has been explained by others. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Bengt Richter |
last post by:
Peculiar boundary cases:
>>> 2.0**31-1.0
2147483647.0
>>> int(2147483647.0)
2147483647L
>>> int(2147483647L )
2147483647
>>>
>>> -2.0**31
|
by: Fred |
last post by:
Why was the bool type introduced into c++?
What does it provide that int does not and are the two entirely
interchangable in conditional expressions?
Thanks
Fred
|
by: Kemal Ozan |
last post by:
Hi,
I am studying K&R book. On the multidimensional Arrays chapter they
say
"int (*daytab)
is a pointer to an array of 13 integers. The parenthesis are necessary
since brackets have higher...
|
by: yuu |
last post by:
Is this code correct C and C++?
{
enum MyEnum { ME_A, ME_B } d = ME_B;
--(int)d;
/* 'd' should be ME_A now. I added the cast because g++ would not
accept the program without it. */
}
|
by: Pokerkook |
last post by:
Hello,
If anybody could help me with this I would greatly appreciate it. Or at least
tell me why I get the output of this garbage:
49
49
10
49
52
|
by: Alfonso Morra |
last post by:
Hi,
I'm getting a compiler error of "pointer truncation from 'void *' to
'int'" because I'm not explicitly casting from void* to (datatype*).
Pointers are stored as ints (is that right?), so as...
|
by: pinkfloydhomer |
last post by:
Is it well-defined to make a cast from a pointer to an int and back?
Like:
typedef struct
{
int whatever;
} S;
int main(void)
{
|
by: jacob navia |
last post by:
Hi
Suppose you have somewhere
#define BOOL int
and somewhere else
typedef BOOL int;
|
by: subramanian |
last post by:
I have been thinking that all pointers(to any obejct) have the same
size. The size of a pointer is the size of an int. This is beause a
memory location is addressed by an int. Is that right/wrong?
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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,...
|
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...
|
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |