What is the value of a pointer if it is pointing to an array or an object?

I know that my question may be very basic, but I wonder for the objects that are getting more than one cell in memory, like arrays and user defined objects (that need more than one cell in memory and thus have the range of consecutive addresses in memory), what does a pointer really mean for this kind of objects? Is it the variable in C++ that contains the address of these objects in memory (logically not true because these objects have occupied more than one cell in memory and thus have range of consecutive addresses), or let's say pointers to these objects are just the beginning address of these objects (more rational).

Please help me understand; and if you are not convinced with my interpretation about C++ pointers definition, give me the right one.

In most C++ tutorials it said that pointers just contain the addresses of other variables in memory.


If I (as the compiler) know that an int is (on this particular system) 4 bytes long, a pointer to an int need only tell me where the "start" of the int is: I just need to read it and the next 3 bytes!

For larger data structures like arrays, the same is true: if I know where my array starts, I know that I can access each subsequent element by adding the size of one item to the address. For example, if I have an int a[] starting at address 100, and an int is 4 bytes, then

a[32] = (address of a) + (32 * size of int) = 100 + 128 = 228 So bytes 228 through 231 are the integer at a[32].

What makes this slightly easier to use is that the compiler abstracts the different sizes of the data types away for us. If I add 1 to my integer pointer, the address actually increases by 4! This is because I very rarely (almost never) want to read half an integer: it's more likely that I wish to look at a series of integers in turn.


Lets say you have a variable a declared and initialized like

int a = 5;

Then you create a pointer and make int point to a using the address-of operator & :

int* pointer_to_a = &a;

The actual value of the pointer_to_a is the address of where a is in memory. But the compiler knows it's a pointer, so you can use pointer_to_a to access the contents of a with the dereference operator * :

*pointer_to_a = 10;
std::cout << "a = " << a << 'n';

The above will print 10 , as you set the contents of where pointer_to_a is pointing to 10 .


From here

The memory of your computer can be imagined as a succession of memory cells, each one of the minimal size that computers manage (one byte). These single-byte memory cells are numbered in a consecutive way, so as, within any block of memory, every cell has the same number as the previous one plus one.

This way, each cell can be easily located in the memory because it has a unique address and all the memory cells follow a successive pattern. For example, if we are looking for cell 1776 we know that it is going to be right between cells 1775 and 1777, exactly one thousand cells after 776 and exactly one thousand cells before cell 2776.]1

链接地址: http://www.djcxy.com/p/5416.html

上一篇: 为什么没有直接访问内存位置,比如Java中的指针?

下一篇: 如果指针指向数组或对象,指针的值是多少?