Storing the address to an array in a pointer

Hello, I'm having trouble understanding the relationship between arrays and pointers. Based on what I've read, arrays are syntax sugar for pointers and arrays simply stores the pointer to the first element.

1
2
3
4
5
// the following works fine
int i_array[] = { 1, 2, 3 };
int *i_array_ptr = i_array;

cout << i_array[0] << " " << *(i_array + 1) << " " << *(i_array_ptr + 2) << endl;


1
2
// this doesn't work
int *i_array_new = { 1, 2, 3 };


Why am I not allowed to directly store the address to an array in a pointer?

Cheers!
because you are trying to point at an array.

the int* can only point to one int at a time.

i_array_new is a pointer so cant be assigned a collection of values, it has to receive an address.

in your working example you assign the pointer to bei_array (notice the lack of []) thats the address of the array. thats why you need +1 +2 etc to access consecutive items.


Last edited on
But doesn't { 1, 2, 3 } return the address of the first value only? So I would have thought int* should be able to store the address to that.
Last edited on
oops, took too long to edit my post.

no, it is an actual collection, &{1,2,3} is the address. but thats invalid c/c++
Topic archived. No new replies allowed.