I'm Java programmer and I'm struggling with this simple stuff.
How can I return this multidimensional array? Does it have to return a ** pointer? How can I get it in another file?
static MoveDirection ghost_moves[GHOSTS_SIZE][4];
MoveDirection** ge开发者_Go百科t_ghost_moves() {
return ghost_moves;
}
A 2D array is not a pointer to a pointer. Arrays and pointers are fundamentally different types in C and C++. An array decays into a pointer to its first element (hence the frequent confusion between the two), but it only decays at the first level: if you have a multidimensional array, it decays into a pointer to an array, not a pointer to a pointer.
The proper declaration of get_ghost_moves
is to declare it as returning a pointer to an array:
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;
}
This syntax syntax is extremely confusing, so I recommend making a typedef for it:
// Define MoveDirectionArray4 to be an alias for "array of 4 MoveDirections"
typedef MoveDirection MoveDirectionArray4[4];
MoveDirectionArray4 *get_ghost_moves() {
return ghost_moves;
}
I believe that you want the following:
MoveDirection ( * get_ghost_moves() ) [GHOSTS_SIZE][4];
In the above get_ghost_moves
is a function returning a pointer to an array of array of MoveDirection
, with dimensions GHOSTS_SIZE
and 4.
I found the following two sites very useful for learning how to understand C declarations:
- Reading C type declarations
- cdecl - converts C declarations into words.
#define MAX 20
char canopy[20][MAX];
typedef char frank[MAX];
frank friend[MAX];
frank *getList()
{
return friend;
}
void test(int n)
{
frank *list = getList();
int i;
for (i = 0; i < 5; i++ )
{
printf("list[%d] %s\n\r", i, list[i]);
}
}
int main(void)
{
int i, nf;
for (i = 0; i < 5; i++ )
{
snprintf(friend[i], MAX, "test%d", i);
printf("test[%d] %s \n\r", i, friend[i]);
}
test(5);
return 0;
}
No, just a single * is all you need. The key is to think about how your data is arranged in memory: an array is just a series of items of the same type, laid out contiguously in memory. In this context it doesn't matter what shape or size the array is - you're just returning a pointer to the start of it, which means you're returning the address of the first item.
Of course, whoever you're returning that pointer to will have to know what the shape and size of the array are.
One of the nice things about C and pointers is that everything's just a number, and memory is just a big array of bytes. Once you get comfortable thinking like that, it all falls into place.
A multidimensional array is not an array of pointers. It is a single memory block. It only has meaning inside the scope where it is declared.
You can't cleanly return that as you have here.. you would have to return a MoveDirection[GHOSTS_SIZE][4]
.
ghost_moves
occupies a contiguous set of GHOSTS_SIZE*4
MoveDirections in memory: its equivalent to a MoveDirection*
It cannot be used with a pointer to pointer, but with a single pointer.
If you want to use two operator[]
-s to index your array you've got two options:
use a variable of type:
MoveDirection (*ghost_moves) [GHOST_SIZE]
and then just doghost_moves[i][j]
this is mostly to have a pointer to pointer, usually is not a good solution: have
ghost_moves
of typeMoveDirection**
for example:
MoveDirection ghost_moves_original[GHOST_SIZE][4]; MoveDirection *ghost_moves[GHOST_SIZE]; ghost_moves[0] = ghost_moves_original; ghost_moves[1] = ghost_moves_original + GHOST_SIZE; ...
精彩评论