Multidimensional array pointer return

I am a Java programmer and I struggle with these simple things.

How can I return this multidimensional array? Do I need to return a ** pointer? How can I get it in another file?

static MoveDirection ghost_moves[GHOSTS_SIZE][4]; MoveDirection** get_ghost_moves() { return ghost_moves; } 
+4
source share
6 answers

A 2D array is not a pointer to a pointer. Arrays and pointers have fundamentally different types in C and C ++. An array decays to a pointer to its first element (hence, a frequent confusion between them), but it only decays to the first level: if you have a multidimensional array, it decays to a pointer to an array, not a pointer to a pointer.

The correct 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; } 

The 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; } 
+5
source

I believe that you want the following:

 MoveDirection ( * get_ghost_moves() ) [GHOSTS_SIZE][4]; 

The above get_ghost_moves has a function that returns a pointer to an array of MoveDirection array with sizes GHOSTS_SIZE and 4.

I found the following two sites very useful for learning how to understand C declarations:

+3
source
 #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; } 
+3
source

No, only one * is all you need. The main thing is to think about how your data is ordered in memory: an array is just a series of objects of the same type laid out adjacent to one another in memory. In this context, it does not matter what the shape or size of the array is - you simply return a pointer to the beginning, which means that you are returning the address of the first element.

Of course, whoever you return this pointer to, you need to know what the shape and size of the array are.

One of the nice things about C and pointers is that everything is just a number, and memory is just a large array of bytes. As soon as you get such comfortable thinking, all this will fall into place.

+2
source

ghost_moves occupies an adjacent set of GHOSTS_SIZE*4 MoveDirections in memory: its equivalent to a MoveDirection*

It cannot be used with a pointer to a pointer, but with a single pointer.


If you want to use two operator[] -s to index your array, you have two options:

  • use a variable like: MoveDirection (*ghost_moves) [GHOST_SIZE] and then just ghost_moves[i][j]

  • this is mainly having a pointer to a pointer, usually this is not a good solution: ghost_moves type MoveDirection**

    eg:

     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; ... 
0
source

A multidimensional array is not an array of pointers. This is a single block of memory. It only makes sense inside the area where it is declared.

You cannot purely return this, as here, you need to return MoveDirection[GHOSTS_SIZE][4] .

0
source

All Articles