假設row = 3
這個例子。
int ***s;
// s=[?]
// s is an uninitialized variable.
s = new int **[row];
// s[*] -> [?]
// [?]
// [?]
// s points to the first element of an array of size 3.
// The elements are uninitialized.
*s = new int *[row];
// s=[*] -> [*] -> [?]
// [?] [?]
// [?] [?]
// We've initialized s[0]. It points to another array of size 3.
// All elements of that array are also uninitialized, along with s[1] and s[2].
**s = new int[row];
// s=[*] -> [*] -> [*] -> [?]
// [?] [?] [?]
// [?] [?] [?]
// More of the same. s[0][0] is initialized.
// This last array contains uninitialized ints, not pointers.
***s = 1;
// s=[*] -> [*] -> [*] -> [1]
// [?] [?] [?]
// [?] [?] [?]
// We traverse three levels of pointers (->) and store 1 in the cell.
所有這些應該編譯和工作正常(只要你不訪問任何未初始化的元素)。
s + 1
指向第一個數組的第二個元素。
// s=[*] -> [*] -> [*] -> [1]
// s + 1 -> [?] [?] [?]
// [?] [?] [?]
*(s + 1)
指細胞[?]
指向s + 1
圖中的上方。此單元格未初始化。
**(s + 1)
試圖取消引用無效的垃圾指針(並且經常崩潰)。
不知道你到底想要什麼。你在尋找一個3d陣列嗎? –
結束遊戲是一個二維數組,並且具有充滿指針的垂直數組和充滿變量的列。 – Mishap
爲什麼不使用'int matrix [x] [y]'來初始化一個二維數組?我不明白你的意思是'垂直數組充滿指針'。 –