0

Suppose I have a 2-D array of floats:

float a[1024][1024];

I want to store the pointer to array

I did:

float** temp = a;

but doesn't seem to work. It gives the error:

main.cpp: In function 'int main()':
main.cpp:105:24: error: cannot convert 'float (*)[1024]' to 'float**' in initialization

   float ** temp = old_array;

Any help is appreciated! Thanks!

Community
  • 1
  • 1
user3059427
  • 209
  • 2
  • 3
  • 10
  • Since this is tagged C++ I would suggest you use `std::array, 1024> a; auto& b = a;` Assuming you have a C++11 compiler. – Borgleader Jan 15 '14 at 02:05

1 Answers1

3

Arrays decay to pointers. In the case of 2D arrays, an array of type T[x][y] will decay into T(*)[y], not to T**.

§ (8.3.4) Arrays:

If E is an n-dimensional array of rank i × j × ... × k, then E appearing in an expression that is subject to the array-to-pointer conversion (4.2) is converted to a pointer to an (n−1)-dimensional array with rank j × ... × k. If the * operator, either explicitly or implicitly as a result of subscripting, is applied to this pointer, the result is the pointed-to (n − 1)-dimensional array, which itself is immediately converted into a pointer.

Your options are to manually reconfigure the type to match the type of the expression...

float (*temp)[1024] = a;

or use a multi-deminsional std::vector or std::array (C++11)...

std::array<std::array<int, 1024>, 1024> a;
auto temp = a;
David G
  • 94,763
  • 41
  • 167
  • 253