Lian Xucan
Lian Xucan

Reputation: 1

Don't know how to extracting two-dimensional arrays in three dimensions C

This is my void:

struct MateMessage newMate(char** cr){ ...... }

And this is my three dimensions args:

char args[3][3][3]

-------The each slot of 'args' if not NULL.---------

And I do like this:

 newMate(args[1])

This is what I get: [Warning] passing argument 1 of 'newMate' from incompatible pointer type

If you need more message please tell me. Thank you.

Upvotes: 0

Views: 46

Answers (3)

Bodo
Bodo

Reputation: 9875

Although a one-dimensional array is much like a pointer to an array element, a multi-dimensional array is not a pointer to a pointer... or an array of pointers. In memory it is organized like a one-dimensional array, the only difference is that the compiler knows how to calculate the address from more than one index.

Your array is organized in memory like this

a000 a001 a002 a010 a011 a012 a020 a021 a022 a100 a101 a101 ...

where a[0][0][0] etc. is written as a000 etc. to save space. You can do

newMate(args[1])

if you define newMate to take a two-dimensional array

struct MateMessage newMate(char cr[3][3]) {
    int i,j;
    for(i=0; i<3; i++) {
        for(j=0; j<3; j++) {
            doSomethingWith(cr[i][j]);
        }
    }
    return something;
}

In this case function the caller will pass the address of a[1][0][0] to newMate and newMate will know how to address the remaining two dimensions.

Upvotes: 0

Lundin
Lundin

Reputation: 214465

For a function accepting an array of type char args[3][3][3], simply use this:

struct MateMessage newMate(char args[3][3][3])

Or in case the dimensions should be variable:

struct MateMessage newMate(size_t x, size_t y, size_t z, char args[x][y][z])

Upvotes: 0

Some programmer dude
Some programmer dude

Reputation: 409364

args[1] is an array of arrays, and an array of arrays is not the same as a pointer to a pointer.

It can however decay to a pointer to arrays. In your case args[1] will decay to the type char (*)[3]. Which is the type you need to use for the argument:

struct MateMessage newMate(char (*cr)[3]){ ...... }

Upvotes: 1

Related Questions