# Thread: passing 3 dimentional array into function

1. ## passing 3 dimentional array into function

hi, i'm looking to pass the array fieldPos[9][50][50] into a function which then changes some of the values of fieldPos[x] (ie want to pass one of 9 possible 50 by 50 maps into the function) and returns the new array.

right now, fieldPos is defined as a global variabe, but i need to change it to a local variable. i've never used pointers, so if i need to use pointers, references would be greatly appreciated.

so, i'm imagining code is something like this...

Code:
char fieldPos[9][50][50];
int x=3;

for (i=0;i<9; i++){
for (j=0; j<50; j++){
for (k=0; k<50;k++){
fieldPos[i][j][k]='x';
}//k
}//j
}//i
and then

Code:
doSmthng (fieldPos[x]);
or something like that ...

but i don't know what the arguments for doSmthng should be or how to pass the array into the function.

any help would be greatly appreciated. thanks

-Sean

2. >ie want to pass one of 9 possible 50 by 50 maps into the function
Then you want to pass a two dimensional array:
Code:
void foo ( char fieldPos[50][50] )
{
// Blah blah

return fieldPos
}

// ...

foo ( fieldPos[x] );
>and returns the new array
There's no "newing" going on here. Arrays are passed by "reference", in that you always access the original data, so you don't need to return anything if all you're doing is modifying the original array.

3. Thanks for the reply ... just to clarify, fieldPos[x] is actually a 2 dimentional 50x50 array??

4. >just to clarify, fieldPos[x] is actually a 2 dimentional 50x50 array??
Multidimensional arrays in C are really just arrays of arrays of arrays of...for however many dimensions you have. So a three dimensional array is an array of arrays of arrays. If you index the first dimension (ie. array[x]), you get an array of arrays, or a two dimensional array in common jargon. If you go on to index one of those (say, array[x][y]), you get a single array. Well, that long winded explanation boils down to yes. So, yes.

Popular pages Recent additions