What does the pointer at the end of the object mean?

In C ++ Primer, chapter 2, “Variables and Core Types,” he says:

You can specify a pointer to an object and a pointer at one end of the end of another object to store the same address.

I am not a native speaker, and I think that why I am a little confused by the sentence "pointer one after the end of the object." Will someone tell me what that means?

+4
source share
3 answers

This means that after

int a[] = { 1, 2 };
float b;

perhaps that (void *) &a[2] == (void *) &bcan be compared as true.

&a[2](or, which is the same thing a+2) is a pointer only for the end a, because the array contains only elements with indices 0 and 1.

( a[2] &a[3]), , , , , , , , , , .

+6

, , int foo[5] = {1,2,3,4,5};. :

-----------
|1|2|3|4|5|
-----------

, ; , ( , , STL ) - . :

-------------
|1|2|3|4|5|?|
-------------
 ^         ^
 |         |
 p         q

p - ; q - --.

, const char bar[3] = "Hi";. , , :

<--foo---> <-bar->
-----------------
|1|2|3|4|5|H|i|0|
-----------------
 ^         ^
 |         |
 p         q

q foo, bar[0].

+8

"one past" means "+1 operation on the pointer." The following code generates:

0x7ffffcf6a848
0x7ffffcf6a84c
0x7ffffcf6a84c

As you can see, the address is "one minus a", i.e. &a + 1matches the address &b.

    int a = 2; 
    int b = 3;
    cout<< &a << endl << &a + 1 << endl << &b <<endl;
+1
source

Source: https://habr.com/ru/post/1527475/


All Articles