我是Java程序员,我正在努力解决这些简单的问题.
我怎样才能返回这个多维数组?是否必须返回**指针?我如何在另一个文件中获取它?
static MoveDirection ghost_moves[GHOSTS_SIZE][4];
MoveDirection** get_ghost_moves() {
return ghost_moves;
}
Run Code Online (Sandbox Code Playgroud)
2D数组不是指向指针的指针.数组和指针在C和C++中基本上是不同的类型.数组衰变为指向其第一个元素的指针(因此两者之间经常混淆),但它只在第一级衰减:如果你有一个多维数组,它会衰变为指向数组的指针,而不是指向数组的指针指针.
正确的声明get_ghost_moves是声明它返回一个指向数组的指针:
static MoveDirection ghost_moves[GHOSTS_SIZE][4];
// Define get_ghost_moves to be a function returning "pointer to array 4 of MoveDirection"
MoveDirection (*get_ghost_moves())[4] {
return ghost_moves;
}
Run Code Online (Sandbox Code Playgroud)
这种语法语法非常混乱,所以我建议为它创建一个typedef:
// Define MoveDirectionArray4 to be an alias for "array of 4 MoveDirections"
typedef MoveDirection MoveDirectionArray4[4];
MoveDirectionArray4 *get_ghost_moves() {
return ghost_moves;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2022 次 |
| 最近记录: |