C & C ++: What is the difference between a pointer and an array address?

C ++ 11 Code:

int a[3]; auto b = a; // b is of type int* auto c = &a; // c is of type int(*)[1] 

C code:

 int a[3]; int *b = a; int (*c)[3] = &a; 

The values ​​of b and c same.

What is the difference between b and c ? Why are they not the same type?

UPDATE: I resized the array from 1 to 3.

+6
source share
3 answers

The sizeof operator must behave differently, for example, if you change the declaration of a to a different number of integers, for example int a[7] :

 int main() { int a[7]; auto b = a; auto c = &a; std::cout << sizeof(*b) << std::endl; // outputs sizeof(int) std::cout << sizeof(*c) << std::endl; // outputs sizeof(int[7]) return 0; } 

For me, this prints:

 4 28 

This is because the two pointers are very different. One is a pointer to an integer, and the other is a pointer to an array of 7 integers.

The second really has a pointer-array type. If you cast it, of course, it will break into a pointer in most cases, but in fact it is not a pointer to a pointer to an int. The first is an int-pointer because the decay occurred on assignment.

Other places that will be displayed are if you really have two pointer-array variables, and tried to assign them to others:

 int main() { int a[7]; int b[9]; auto aa = &a; auto bb = &b; aa = bb; return 0; } 

This brings me an error message:

 xx.cpp: In function 'int main()': xx.cpp:14:8: error: cannot convert 'int (*)[9]' to 'int (*)[7]' in assignment aa = bb; 

This example, however, works because dereferencing bb allows it to decay into pointer-to-int:

 int main() { int a; int b[9]; auto aa = &a; auto bb = &b; aa = *bb; return 0; } 

Note that decay does not occur on the left side of the assignment. This does not work:

 int main() { int a[7]; int b[9]; auto aa = &a; auto bb = &b; *aa = *bb; return 0; } 

This will work:

 xx2.cpp: In function 'int main()': xx2.cpp:14:9: error: incompatible types in assignment of 'int [9]' to 'int [7]' *aa = *bb; 
+7
source

The identity of any object in C ++ is determined by a pair of its type and its address.

In your example, there are two different objects with the same address: the array itself and the first element of the array. The first is of type int[1] , the second is of type int . Two different objects can have the same address if one is a subobject of the other, as is the case with array elements, class members, and class subobjects.

Your example will be clearer if you wrote:

 int a[5]; int (*ptr_to_array)[5] = &a; int * ptr_to_array_element = &a[0]; 

But you took advantage of the fact that the id-expression a for the array splits into a pointer to the first element of the array, so a has the same meaning as &a[0] in your context.

+6
source

Consider the following example:

 #include<stdio.h> int main() { int myArray[10][10][10][10]; //A 4 Dimentional array; //THESE WILL ALL PRINT THE SAME VALUE printf("%d, %d, %d, %d, %d\n", myArray, myArray[0], myArray[0][0], myArray[0][0][0], &myArray[0][0][0][0] ); //NOW SEE WHAT VALUES YOU GET AFTER ADDING 1 TO EACH OF THESE POINTERS printf("%d, %d, %d, %d, %d\n", myArray+1, myArray[0]+1, myArray[0][0]+1, myArray[0][0][0]+1, &myArray[0][0][0][0]+1 ); } 

You will find that all 5 values ​​printed in the first case are equal. Because they point to the same starting location.

But when you increment them by 1, you see that different pointers are now moving (points) to different locations. This is due to the fact that myArray[0][0][0] + 1 will jump by 10 integer values, equal to 40 bytes, while myArray[0][0] + 1 will jump by 100 integer values ​​ie by 400 bytes . Similarly, myArray[0] + 1 jumps to 1000 integer values ​​or 4000 bytes.

The values ​​depend on the level of the pointer that you have in mind.

But now, if I use pointers to reference all of them:

 #include<stdio.h> int main() { int myArray[10][10][10][10]; //A 4 Dimentional array; int * ptr1 = myArray[10][10][10]; int ** ptr2 = myArray[10][10]; int *** ptr3 = myArray[10]; int **** ptr4 = myArray; //THESE WILL ALL PRINT THE SAME VALUE printf("%u, %u, %u, %u\n", ptr1, ptr2, ptr3, ptr4); //THESE ALSO PRINT SAME VALUES!! printf("%d, %d, %d, %d\n",ptr1+1,ptr2+1,ptr3+1,ptr4+1); } 

So, you see that different levels of pointer variables do not behave like an array variable does.

+1
source

All Articles