Kyle Blue Kyle Blue - 9 months ago 41
C++ Question

What is a pointer to an array of x amount of ints?

Sorry if this may seem like a stupid question, however, ive been working through C++ Primer, and I feel as if something hasn't been explained clearly...
(PS ints are just a generic example)

I understand what it means to have a array of pointers to ints e.g.

int *foo [4];


int *foo1 [2][4];

however, i don't understand what it means to have a pointer to an array of x ints. Example from book:

int ia[3][4];
int (*p) [4] = ia;

It would be great if you could give examples for both 1 dimensional and multidimensional arrays... Thanks!

Answer Source

When you declare a variable like

int array[137];

the type of the variable array is int[137]; that is, an array of 137 ints. In C++, you can create pointers or references to variables of any type that you'd like, so you can create a pointer to the variable array. Since array has type "array of 137 ints," a pointer to array would have type "pointer to an array of 137 ints." The syntax for writing out a pointer like this would be

int (*arrayPtr)[137] = &array;

This is read as "arrayPtr is a pointer, and what it points at is an array of 137 ints." The syntax here is unusual, but that's what you would do if you wanted a pointer to an array of 137 integers.

Similarly, you could make a reference to array like this:

int (&arrayRef)[137] = array;

It's extremely uncommon to see pointers to arrays actually used anywhere - I've never seen it done except in very specialized template circumstances. References to arrays are sometimes used in template programming, but otherwise I've never seen them used anywhere before.

In other words, it's good to know these exist and know how to read them, but realistically you're unlikely to need them unless you start doing some pretty advanced library development or like to play around with template wizardry.

This gets weirder when you factor array-to-pointer decay into the mix. The example code you had was essentially

int myArray[137][42];
int (*arrayPtr)[42] = myArray;

The type of arrayPtr is "pointer to an array of 42 ints", which is of type int (*) [42]. So why can we assign myArray, which has type int[137][42], to it? This is where array-to-pointer decay kicks in. Array-to-pointer decay is an implicit conversion that converts an array to a pointer to its first element. Let's see how that applies here.

myArray is an array of 137 arrays of 42 integers. This can be though of as "an array of 137 things, each of which is an int[42]." This means that when array-to-pointer decay applies to myArray, it converts to a pointer to its first element. That element is itself an array of 42 integers, so the effect of applying the array-to-pointer decay is that the expression myArray implicitly converts to an int (*) [42] (a pointer to an array of 42 integers), specifically, one pointing at the first row in the array.

The net effect of this assignment is that arrayPtr now points to the first of the 137 arrays in myArray. This is just plain weird, if you ask me, and I would not advise writing code like this. I've been writing C++ for years and never had the misfortune of seeing this used anywhere, so I think it's safe to chalk this one up to "bizarre edge cases that only library implementers need to worry about." :-)