the * symbol, when used in conjunction with a data type is
always a pointer, and never an array. This rule goes without exception.
Unfortunately, C and C++ allow pointer syntax and array syntax to be mixed freely, allowing a programmer to create rather deceptive code which, on the face of it, creates a blurry distinction between arrays and pointers.
Often, a pointer variable is used to store the address of the
first element in an array (Which can be used to calculate subsequent elements). Here is an example where pointer syntax and array syntax can be used interchangably, yet, on the face of it, you could be forgiven for thinking that the two are completely different
Code:
#include <iostream>
int main()
{
int my_array[] = { 1, 2, 5, 10, 20, 50, 100, 200, 500 };
int* my_pointer;
my_pointer = my_array;
std::cout << my_pointer[0] << std::endl;
std::cout << *my_pointer << std::endl;
} the above program should output
Whenever you see anyone mention anything to do with "equivalence" between pointers and arrays, this is what they are talking about. More precisely, they should say that the
syntax is equivalent. Arrays and pointers themselves are completely different.
Just to prove that i'm not making this up, here's the same scenario involving the 4th element in the array
Code:
#include <iostream>
int main()
{
int my_array[] = { 1, 2, 5, 10, 20, 50, 100, 200, 500 };
int* my_pointer;
my_pointer = my_array;
std::cout << my_pointer[3] << std::endl;
std::cout << *(my_pointer+3) << std::endl;
} The last line of the program may look a little unusual if you're uneasy with pointers - though it has the same effect as
my_pointer[3], just using pointer arithmetic, rather than array subscript notation
- Pointer arithmetic is a facility which C/C++ provides in order to take any address in memory, and step over a certain number of bytes in order to reach some other address. The smallest number of bytes which you may step over in any single calculation is exactly equal to the
sizeof the pointed-to object.
- for
int* the "step size" in bytes is
sizeof(int)
- for
char* the "step size" in bytes is
sizeof(char)
etc
So, remembering that
my_pointer merely stores an address in memory - the program's code shows that my_pointer has type
int*. When adding 3 to the address stored in
my_pointer, the final address value will be the value of
my_pointer plus 3 lots of
sizeof(int)
if my_pointer happened to be an address value of 02000 (decimal), and sizeof(int) happened to be 4, the final address value would be 02012 decimal
(Although its extremely rare to actually need to know, or care, what the absolute address values are, since memory is allocated at runtime)
- Back on topic as to how this fits in with arrays; The most important feature of an array is that arrays are
contiguous memory blocks. This is important, because, if you know the size of the individual elements, and you know the address of the first element, then pointer arithmetic empowers you to calculate the addresses of all other elements within your array.
This is essentially how your CPU 'knows' where each element of an array is - But, luckily, C and C++ don't require the programmer to get involved, because subscript notation does it all for you!