3

I have a very basic but haunting problem about pointer and array:

int main() {

    int a[5] = { 1,2,3,4,5 };
    int(*pa)[5] = &a;

    std::cout << a << std::endl;
    std::cout << &a << std::endl;

    std::cout << pa << std::endl;   
    std::cout << (*pa) << std::endl;

    return 0;
}

Surprisingly, all four outputs give the same address, something like '006AF784', which means a == &a and pa == *pa. This does not make any sense to me!

I understand of course 'a' is the pointer to the first element while '&a' is the pointer to the whole array, so 'a+1' is different from '&a+1'. But a variable is equal to its address and a pointer is equal to the content which points to is not understandable to me. I wonder what is exactly going on within C and compiler.

0

3 Answers 3

1

Implicit conversion and "arrays decaying into pointers" is behind this.

Let's draw this array. Assume that it's stored beginning at address 0x98.

 +———————————————————+
 | 1 | 2 | 3 | 4 | 5 |
 +———————————————————+
 ^
 |
0x98

It should be clear that the address of the array is 0x98.
It's pretty clear that the address of its first element is also 0x98.

When you're printing

std::cout << a << std::endl;

a is converted into a pointer to its first element – it is equivalent to

std::cout << &a[0] << std::endl;

As illustrated above, this has the same numeric value as the pointer to the array.

Likewise, when you print

std::cout << (*pa) << std::endl;

*pa, being an array, is converted into a pointer to its first element.

Sign up to request clarification or add additional context in comments.

2 Comments

It's likely that variable 'a' should be think of as an object which contains 5 ints. So std::cout<<a is converted to count<<&a[0] to print the address of the first integer, and std::cout<<&a prints the address of the whole object which starts of course from its first element.
@Roy It's not only likely, it's a fact. That's what arrays are.
1

You pretty much answered your own question. You want to know why a, &a are the same: the first expression designating the array evaluates to a pointer to the first element and the other evaluates to a pointer to the whole thing, as you note. But both are the same address: the first element is at the base address of the array. Then why is pa the same? Why, because you initialized it from &a in its declaration; it got its value from &a. And *pa is the same because pa is a pointer to the array, so *pa is the array. But an array evaluates to a pointer to the first element: and you have already seen this with a. The expression *pa designates the same object as a, has the same type and evaluates the same way.

Comments

1

It's important to understand that a isn't a pointer. In the semantics of C, the array name is convertible to a pointer, but is otherwise just an alias of the address of the first element of the array.

Then when it comes to pa, you're just saying that this pointer should be the address of a, so of course when you print it's value, it should be the same. And of course since *pa is an array (its array name), it just aliases its first element's address - which is also a's.

2 Comments

I think your first sentence is probably where the OP has the problem. a is an array variable, which, as you say, is convertible to a pointer, but simply holds the first element of the array is its value.
@DavidC.Rankin: Yes, a is an array variable. More precisely, a is the name of an array object. But a doesn't just hold the first element of the array; the value of a is the value of the entire array object, consisting of the values of all its elements. The expression a is, in most contexts, implicitly converted to a pointer expression, yielding the address of the array object's initial element.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.