Sure.
Yes, it's important to learn the fundamentals of arrays and pointers before learning the ways to avoid having to use them :)
The only way I can keep them straight is to understand how the memory is laid out in each case.
For example,
1 2 3
|
char* array = new char[ 10 ];
for( int i = 0; i < 10; ++i )
array[ i ] = i;
|
array is a variable which contains a (32-bit) pointer. The array variable might be stored at memory location 0x1000. new char[] might have returned the pointer 0x2000. So in memory, if I look at memory locations 0x1000-0x1003, they will contain the value 0x00002000. If I
cout << array
, I'll get 0x1000. And if I look at memory locations 0x2000-0x2009, they will contain the values 00 01 02 03 04 05 06 07 08 09.
Contrast that to
1 2 3
|
char array[ 10 ];
for( int i = 0; i < 10; ++i )
array[ i ] = i;
|
Same code, except now array is an array instead of a pointer to an array.
array is now a 10-byte "variable" which might be stored in memory at locations 0x2000-0x2009. If I
cout << array;
I'll get 0x2000. And if I look at memory locations 0x2000-0x2009, I'll see 00 01 02 03 04 05 06 07 08 09.
See the difference? In the first example, array is a pointer that occupies 4 bytes of memory. The value stored there is the address of the first element of the array. In the second example, there is no pointer stored in memory; rather when I reference "array" in code, this is automatically the address of the first element of the array. One less level of indirection.
That's the way I keep pointers and arrays straight, and it explains why arrays and pointers aren't quite the same thing, but they are very similar.