0

Consider the code :

const int MAX = 3;
int (*array)[MAX]  ;

int intArray[3] = {30 , 40 , 50};

array = &intArray;
for (int h = 0 ; h < MAX ; h++)
{
    std::cout << *(array[h]) << std::endl;
}

The output is :

30
0
3

Clearly something is wrong with the initialization of the array , why do I get 30 , 0 , 3 and not 30,40,50 ?

5
  • 4
    Did you mean (*array)[h]?` Commented Sep 27, 2015 at 17:34
  • 2
    In C++ you can use std::array<> Commented Sep 27, 2015 at 17:40
  • 1
    An array is an alias for a pointer in C++. While you can have a pointer to a pointer, there’s a good chance you really wanted int *arrayptr = array; Commented Sep 27, 2015 at 17:46
  • @Lorehead An array is not an alias for a pointer in C++, they are of different types. Commented Sep 27, 2015 at 18:10
  • They’re not exactly equivalent in all circumstances, but you can implicitly convert the name of an array to a pointer. &array[0] literally does have pointer type, if you need that. Commented Sep 27, 2015 at 18:17

4 Answers 4

5

Just like that:

const int MAX = 3;
int *array;

int intArray[3] = {30 , 40 , 50};

array = intArray;
for (int h = 0 ; h < MAX ; h++)
{
    std::cout << array[h] << std::endl;
}
Sign up to request clarification or add additional context in comments.

Comments

2

The handling of built-in arrays in C++ is quite peculiar, especially when considering pointers. A pointer can be seen as one of two things:

  • as pointer to a single object
  • as pointer to the first element of an array

So how does this work? Well, let's start with the definition of your array, and let's define a pointer to the array in the way it is normally done in C++:

const int MAX = 3;
int *array;                           // (1)

int intArray[3] = {30 , 40 , 50};

array = intArray;                     // (2)
for (int h = 0 ; h < MAX ; h++)
{
  std::cout << array[h] << std::endl; // (3)
}

Now what is happening here?

At line (1) we define a pointer to int named array. Note: Not specifically a pointer to array of int, but a pointer to plain int.

At line (2) we assign intArray to that pointer. Now intArray is an array, so how can we assign it to a pointer? Well, here comes the magic: If an array is used in an rvalue context (that is, in a context where a value is required), then the compiler actually provides a pointer to the first element. That is, the line is completely equivalent to

array = &(intArray[0]);

Now in line (3) we access array[h]. That is, we access the pointer as if it were an array. So what happens here? Well, the pointer is interpreted as pointing to the first element of an array, and the element h of that array is accessed. What actually happens is that the compiler replaces array[h] by *(array + h). That is, it dereferences a pointer that is h ints later than array.

OK, so what does your definition

int (*array)[MAX];

do? Well, just as the previous declaration defined a pointer to int, this defines a pointer to an array of MAX ints, that is, a pointer to int[MAX]. As before, this can point either to a single array of three ints, or to an array of such arrays.

Now when you did

array = &intArray;

you set it to point to the single array intArray.

Now in your output line you wrote:

*(array[h])

So what happens here? Well, in the parentheses you have array[h], that is, you've used the array subscript operator [] on the pointer array. Therefore the compiler will access the h-th array of three ints in your array of arrays of three ints. Now you only have one array of ints there, so as soon as h > 0 you'll be out of bound. There's absolutely no way to predict the numbers you'll get there (your program could also just have crashed).

However we're not yet finished: Your full expression reads *(array[h]), so you actually dereference the result of array[h]. But the result of array[h] is an array of 3 ints! Well, the dereference operator expects a pointer value, therefore again the array "decays" to a pointer to its first element, and that of course can be dereferenced, giving the first element of the array. Indeed, you could have equivalently written *(array[h]) as array[h][0].

And this is also the use case of variables of this type: It would normally be used an variables of the form

int intArray[3][3] = {{30, 40, 50}, {300, 400, 500}, {3000, 4000, 5000}};

where you would initialize the pointer with array = intArray and then your loop would output

30
300
3000

OK, but how would you access the values of your intArray with only three values? Well, for that you have to first dereference the pointer in order to get to the array, and then access the array index operator:

std::cout << (*array)[h] << std::endl;

Alternatively you can see it as the first (and only) row of a 1×3 array, and equivalently write

std::cout << array[0][h] << std::endl;

1 Comment

From this answer's length and topic, I fully expected it to consist almost entirely of total abject nonsense. I was pleasantly surprised. Good job! +1
1
int (*array)[MAX]  ;

This line declare array as pointer to array of MAX integers. so if you want to print intArray using array , you can do something like this

for (int h = 0; h < MAX; h++)
    {
        std::cout << (*array)[h] << std::endl;
    }

1 Comment

int (*array)[MAX]; is not an array of pointers of size 3. array is a pointer with type int (*)[MAX]; An array of pointers of size 3 is int* array[MAX];
0

You should use (*array)[h]. array is a pointer to the intArray, you should dereference it first.

Since someone thinks that array is of array type which is completely wrong. you can verify with the program

#include <type_traits>
#include <iostream>
using namespace std;
int main() {
  cout << is_array<int*[3]>::value << endl;
  cout << is_array<int(*)[3]>::value << endl;
  return 0;
}

5 Comments

array is not a pointer to intArray, it is an array of pointers.
@LuciaPasarin Do you read the program in my answer? array is not an array of pointers.
Sorry, my comment was before looking at the last edit with the code.
Really useful method to know if a certain type is an array, btw. Why cannot we write int *(array[MAX]) instead of int (*array)[MAX]? It would make it clearer to me that array is a pointer then.
@LuciaPasarin It's just syntax. but there is some "reason" behind that. when you define an array, you use something like int a[3]; a is an array. int (*pa)[3]; pa is an pointer to array, and *pa will be an array conceptually, right?. So a and *pa should be in the same position in the syntax.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.