About pointers and arrays in C ++

I would like to ask a question about pointers and arrays in C ++.

int a[10]; int *p1; p1 = &a[0]; int *p2; p2 = a; int (*p3)[10]; p3 = &a; 

What is the difference between p1, p2 and p3? They are very confusing.

+4
source share
3 answers

First, a is an array of 10 int s. This is the easy part.

p1 is a "pointer to int ". You assign it the value &a[0] . This takes the address of the first element a . So p1 now points to the first element of a .

p2 also a "pointer to int ". You assign a directly to him. In this case, the standard conversion should take place with an array-to-pointer conversion. In principle, an array can be converted to a pointer to its first element. You assign the result of this transformation p2 . Thus, p2 also a pointer to the first element of a .

p3 is a "pointer to an array of 10 int ". You take the address of array a and assign it to this pointer. So, now this pointer points to the array itself (and not to its first element).

You might think: "Well, the first element has the same address as the array, so what's the difference?" In fact, you will notice the difference when you try to increase the pointer. An increment of either p1 or p2 will give you a pointer to the second element of the array. The increment p3 will give you a pointer to the next array of 10 int (which does not actually exist).

 ┌─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┐ │ intintintintintintintintintint │ └─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┘ ^ └── p1, p2, p3 

So, if you start with the directions you described, and then increase them, you get:

 ┌─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬┄ │ intintintintintintintintintint │ └─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴┄ ^ ^ └── p1, p2 └── p3 
+9
source

p1 points to the first element of a .

p2 causes a break into a pointer to the first element, so it also points to the first element of a .

p3 reads as a pointer to an array of ten integers. It points to a itself, and its increment will move forward sizeof(int) * 10 instead of sizeof(int) .

+5
source

There is no difference between these pointer declarations. All of them point to the first element of the array a [] (index 0). They represent only different styles of writing instructions.

0
source

All Articles