3

I know that in C++, the name of an array is just the pointer to the first element of the array. For example:

int foo[5];

=> foo is a pointer of type int, and it points to the first element in the array, which is a[0]

But then, we have this:

int (*foo)[5]

Here, foo is a pointer to an array of type. So is foo a pointer to a pointer (of type int)?

4
  • 2
    No, it's a pointer to an array. Arrays are not pointers. Commented Oct 17, 2012 at 3:35
  • This has to be a dupe, right? Commented Oct 17, 2012 at 3:37
  • @CarlNorum, Oh, let me count the ways. Commented Oct 17, 2012 at 3:39
  • Arrays are pointers with a bit more overhead like array size and padding. Commented Oct 17, 2012 at 4:39

2 Answers 2

5

This is an array of five ints:

int a[5];

This is a pointer to an array of five ints:

int (*p)[5] = &a;

This is a pointer to an int:

int * q = a;  // same as &a[0]
Sign up to request clarification or add additional context in comments.

Comments

2

in C++, the name of an array is just the pointer to the first element of the array

That's not correct: a name of an array can be converted to a corresponding pointer type "for free", but it is definitely not a pointer. The easiest way you can tell is by comparing sizes of a pointer and of an array:

int foo[5];
int *ptr;
cout << sizeof(foo) << endl;
cout << sizeof(ptr) << endl;

Your second example is a pointer to an array of ints of size 5.

cout << sizeof(foo) << endl;
cout << sizeof(*foo) << endl;

In a 32-bit environment this prints 4 and 20, as expected.

4 Comments

So in the second case, if it's a pointer to an array, what is the type of the pointer? Is it just "array"?
@Chin Its type is "a pointer to an array of integers of size five" (pretty long name, huh?).
@Chin Take a look at this link to ideone that illustrates the point from my last comment.
Thanks. In my second example, I just want to know from a memory-diagram perspective whether it is a pointer to a pointer. But since it's not a pointer in the first example... I think I get the idea

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.