1. ## A question about pointers and arrays

As I'm reading "Jumping into C++" and liking the way it is written, I've come across a point that I don't think I understand properly. It says:

assigning an array to a pointer:

Code:
```int numbers [8];
int *p_numbers = numbers;```
using it like an array:
Code:
```for (int i = 0; i < 8; i++)
{
p_numbers[i] = i;
}```
Here's the confusing part:
"The array numbers, when assigned to a pointer, acts as though it is just a pointer. Arrays are not pointers, but can be assigned to pointers."

Am I missing something here? Because it looks to me that the pointer p_numbers acts as an array not the other way around... Please if somebody can clarify this...

2. Line 2 - what is the type of numbers? It's int[8].What is the type of p_numbers? It's int*. Do they differ? Yes. And yet - you can make that assignment. That's what the book is addressing by "Arrays are not pointers, but can be assigned to pointers."

Now, what happens "under the hood" is that if in certain (you could say most) contexts you drop an array name like that - it "decays" to a pointer, or in other words: the array-of-T gets converted to a pointer-to-T that points at the first element of the array.

Here, a bit of standardese:
4.2 Array-to-pointer conversion [conv.array]
1 An lvalue or rvalue of type “array of N T” or “array of unknown bound of T” can be converted to a prvalue of type “pointer to T”. The result is a pointer to the ﬁrst element of the array.
Is it gibberish to you? Even if, take what's important out of it, and realize that there's a lot to be learned about the nitty gritty that you can for the moment ignore.

About the brackets, again, standardese bit:
5.2.1 Subscripting [expr.sub]
1 [...] One of the expressions shall have the type “array of T” or “pointer to T” and the other shall have unscoped enumeration or integral type. The result is of type “T”. The type “T” shall be a completely-deﬁned object type.
The expression E1[E2] is identical (by deﬁnition) to *((E1)+(E2))
So you could say that [] working with arrays and pointers is actually just a syntactic sugar. That's also why stuff like this works:
Code:
```int a[5] = { 0 };
1[a] = 5; // <-- good job obfuscating your code. ;)```

3. IMO, I would strongly suggest you get a better book (e.g. Accelerated C++), because this book do your a disfavour by trying to learn you all kinds of stuff first that you later have to unlearn.

4. Originally Posted by Xupicor
Line 2 - what is the type of numbers? It's int[8].What is the type of p_numbers? It's int*. Do they differ? Yes. And yet - you can make that assignment. That's what the book is addressing by "Arrays are not pointers, but can be assigned to pointers."
Thank you, it's much clearer, but this second part with the brackets isn't so for now I'm gonna ignore it :P

5. Thanks for the suggestion, I'll have a look