2014-01-15 90 views
0

假設我有花車的2-d數組:什麼是存儲二維數組指針的方法?

float a[1024][1024]; 

我想指針存儲陣列

我所做的:

float** temp = a; 

但似乎並沒有工作。它給出的錯誤:

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

float ** temp = old_array; 

任何幫助表示讚賞!謝謝!

+0

由於這是C++的標籤,我建議你使用'std :: array ,1024> a; auto&b = a;'假設你有一個C++ 11編譯器。 – Borgleader

回答

3

數組衰減爲指針。在2D陣列的情況下,T[x][y]類型的數組將衰減到T(*)[y],而不是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.

您的選項是手動重新配置,以匹配表達式的類型的類型...

float (*temp)[1024] = a; 

或使用多deminsional std::vectorstd::array(C++ 11)...

std::array<std::array<int, 1024>, 1024> a; 
auto temp = a;