2014-10-20 171 views
0

我想將指針(Bar *)存儲在2維的多維數組中。我想從指針訪問這個數組。 所以我想初始化一個** foo [] [](或**** fo)。初始化多維數組指針****

Bar ****_bars; // declaration 
int n, m; // dimensions size 

_bars = new Bar ***; 
*_bars = new Bar **[n]; 
for (int i = 0; i < n; i++) { 
    *_bars[i] = new Bar *[m]; 
    for (int j = 0; j < m; j++) { 
     *_bars[i][j] = new Bar(); 
    } 
} 

我在線路*_bars[i][j] = new Bar();當i = 0和j = 1的應用程序崩潰。

+0

爲什麼不使用矢量>? – 2014-10-20 19:02:03

+0

'酒吧**** _酒吧;'今天最大的星星問題,: - P! – 2014-10-20 19:03:03

+0

'std :: vector'沒用,我的數組是靜態的。 – Sony 2014-10-20 19:05:01

回答

0

您的問題可能是運營商[]*的優先級(請參閱this answer)。例如,你的情況表達*_bars[i]進行評估,如:

*(_bars[i]) = .... 

由於在_bars[]只有一個元素你_bars[1]遇到「問題」是什麼。與*_bars[i][j]一樣。

但是,真的,請使用std::array<>std::vector<>這樣的事情。