Can someone please explain to me the difference between the two? Any help is very much appreciated.
2 Answers
A pointer is a place in the memory with an address stored. An array is a place in memory with a certain number of the same type of objects in a row.
Creating an array allocates space for the objects. Creating a pointer allocates space for an address to something else to be stored (or its own address, possibly.)

- 3,624
- 1
- 21
- 35
-
Close. A pointer *contains* an address. A pointer points to *something*. An array is container of *something*. – Thomas Matthews Mar 25 '14 at 23:22
-
A pointer does not need to actually be pointing to something. C/c++ pointers aren't safe and could just have been given random int values or be left uninitialized. – Jules G.M. Mar 25 '14 at 23:28
-
Same goes for c arrays or c++ arrays for types that don't construct themselves (c types). You have no guaranty that there is actually anything contained by the array other than garbage memory. The space is allocated, and you then have to make it into something yourself, unless of course it's an array of c++ objects with constructors. – Jules G.M. Mar 25 '14 at 23:36
int foo[5];
When an array is not used as a value its name represents the whole array thus foo represents the whole array. This array is allocated in the stack memory, you could also use pointers and dynamic memory.
Warning Below
int *foo; // in here this pointers points to nothing
// you can go ahead an use the pointer and see an amazing crash
// or point it to something.
foo = new int[5];
This is problematic because you have elements that are allocated in memory and you need to keep track of the memory you are allocating.
Instead you can declare a pointer to an array of 5 elements.
int (*foo)[5];
foo = new int[5];
As you see, both are pointers, and both are arrays. But you could do something like this:
int p = 5;
int *foo = &p;
This way foo
points to the address of p, which is 5, this way, you can point to other objects.
So a pointer
does not necessarily points to an array
.
C++11
provides great ways to implement your pointers using the std
, these are containers like array
, vector
, list
, among others and shared_ptr
, unique_ptr
.
This way you let the type handle the memory and size of the memory allocated.

- 1
- 1

- 3,023
- 1
- 21
- 48
-
I did the same thing but with boxes, you think is it more clear now? – Claudiordgz Mar 25 '14 at 23:28
-
In the first example, `foo` is an array, not a pointer. Research "pointer decomposition array". – Thomas Matthews Mar 25 '14 at 23:31
-
In the second example, you should use a type "pointer to array of 5 integers", not a pointer to one integer. – Thomas Matthews Mar 25 '14 at 23:33
-
Syntax for pointer to array of 5 integers int (*ptr)[5]; This is similar to a 2d array, for example, ptr[1][2] is the 3rd element in the 2nd array. I don't think this is what the OP is looking for. – rcgldr Mar 25 '14 at 23:38
-
edited, for all your points, just in case this question survives deletion. – Claudiordgz Mar 25 '14 at 23:43