The difference between `a` and` & a` in C ++, where `a` is an array

I am confused about the output of the following code.

#include<iostream> #include<cstdlib> using namespace std; int main() { int a[] = {1,2,3}; cout << a << " " << &a << endl; cout << sizeof(a) << " " << sizeof(&a) << endl; return 0; } 

Output signal

 0xbfcd3ae4 0xbfcd3ae4 12 4 

How do a and &a print the same expression but have different sizes? I always thought that for any array, its name always has value = address of the first byte.

Also, &a does not make sense, since it is impossible to have an address (obtained using the and operator) for an address (value a). However, the code gives the output and infact 'a == & a' according to the output.

Similarly, why the output sizeof(a) = 12 (which is the total occupied memory) over the array? a is the pointer itself sizeof (a) = 4 bytes (on my 32-bit Ubuntu 11.04)

Obviously, there are some misconceptions that I have. Can anyone figure this out?

+7
c ++ arrays
source share
3 answers

The array is not a pointer, but the array tries to jump to the pointer when you try to use it as one. In your case, printing the address of an array automatically converts it to a pointer.

There is little difference between an automatically converted pointer and an explicitly created using & , except that it is a pointer to one element and the other a pointer to the entire array. If you used &a[0] then they would be identical.

+13
source share

First of all, understand that there is a difference between object 1 and the expression that we use to refer to this object. In your code, a is an expression that refers to an object large enough to hold 3 int values.

sizeof it is an operand of sizeof or unary & operators, or is a string literal used to initialize another array in a declaration, an expression of the type "N-element array of T " will be converted ("decay") to an expression of the type "pointer to T ", and the value of the expression will be the address of the first element of the array.

Given a type statement

 cout << a; 

expression a is of type "3-element int array"; since a not an operand of sizeof or unary & operators, it will be converted to an expression of type "pointer to int ", and the value of the expression will be the address of the first element in the array.

OTOH given type statement

 cout << &a; 

the expression a is the operand of the unary operator & , therefore, the rule does not apply; instead, the type of the expression is a "pointer to a 3-element int array", and the value of the expression is the address of the array.

In both C and C ++, the address of the array and the address of the first element of the array are the same, therefore both expressions give the same value, but the types of the two expressions are different ( int * vs. int (*)[3] ).

In a statement

 cout << sizeof a; // or sizeof (a) 

the expression a is the operand of the sizeof operator, so again the conversion rule is not applied; instead, the expression sizeof a calculates the number of bytes used by the array (in this case 12).

In a statement

 cout << sizeof &a; // or sizeof (&a) 

expression &a evaluates the address of the array and is of type int (*)[3] , so sizeof &a evaluates the number of bytes used by the pointer type (in your case, 4 bytes).

In C and C ++, when you declare an array like

 int a[3]; 

the only storage allocated for the three elements of the array; there is no separate storage for the variable a , which points to the first element of the array (which is why a and &a give the same value).


1. In the sense of something that takes up memory, not the meaning of an instance of a class
+6
source share

In C, the expression a is a primary expression that denotes an array object. In the context where this expression is evaluated, it produces a value that is of type pointer, and points to the first element of the array.

This is a special evaluation rule for arrays. This does not mean that a is a pointer. This is not true; a is an array object.

When the sizeof & operators are applied to a , it is not evaluated. sizeof creates the size of the array, and & takes its address: it creates a pointer to the array as a whole.

The values โ€‹โ€‹of the expressions a and &a indicate the same location, but have a different type: a pointer to the type of the array element (for example, a pointer to int ) compared to the type of array (such a pointer to an array of 10 int), respectively.

C ++ works very precisely in this area for compatibility with C.

In C, there are other situations where the value of an expression that denotes a value or object of one type produces a value of another type. If c is of type char , then c is an int . However, &c is of type char * , and sizeof c produces 1. Just as an array is not a pointer, c not int ; he just produces one.

C ++ is incompatible in this area; character expressions such as char variable names or character constants such as 'x' are of type char . This allows void foo(int) and void foo(char) be different overloads of the foo function. foo(3) will call the first, and foo('x') second.

+1
source share

All Articles