# Thread: Passing 2D arrays between functions

1. ## Passing 2D arrays between functions

Im a bit confused how I would do the following:
I have the following functions:
Code:
```void bkSub(char arrayIn[])
void blackNWhite()
void cC()```
Now this is what the flow I am looking for:

bkSub()
This function after receiving the arrayIn and processing it should pass a new 2D array (float array[height][width]) it created down to blackNWhite()
blackNWhite()
This function now receives that 2D array (float array[height][width]), processes it and passes it down to cC()
cC()
This now receive the 2D array as above and processes it and then passes back to main a structure info.

I am not sure how to do the passing of the 2D array between the functions.
Could someone help?

2. Pass it with pointers.

3. Yea I know that but not sure how with 2D arrays

4. void blackNWhite(float array[height][width])
Rise and repeat. Know that you must specify the outer dimension for it to compile.

5. Originally Posted by Elysia
void blackNWhite(float array[height][width])
Rise and repeat. Know that you must specify the outer dimension for it to compile.
Yuck.

Code:
`void blackNWhite(float * array, const size_t rows, const size_t columns)`
Yum.

6. I tried this basic one:
Code:
```void func(char array[])
{
char name[255];
strncpy(name, array, 255);
printf("%s\n", name);
int twoD[5][5];
int i, j;
for(i=0; i<5; i++)
{
for(j=0; j<5; j++)
{
twoD[i][j] = i;
}
}
func2(twoD);
}

void func2(int array[][5])
{
int i, j;
for(i=0; i<5; i++)
{
for(j=0; j<5; j++)
{
printf("%d\n", array[i][j]);
}
}
}```
But i get the error:
Code:
```func.h:28: warning: conflicting types for ‘func2’
func.h:25: warning: previous implicit declaration of ‘func2’ was here```
Actually works now, but how could I do it by passing via pointers?

7. Originally Posted by zacs7
Code:
`void blackNWhite(float * array, const size_t rows, const size_t columns)`
Yuck. 2D arrays as 1D array.

Code:
`void blackNWhite(float array[height][width])`
2D array as 2D array. Yum.

As for the question, cast the array to float* when you pass it. Pass along the rows and columns to the function, too.
And as for the warning, you forgot to make a prototype for the function.

8. Implicit declaration == "I forgot to write a prototype for that function."

If you want to write a function that can handle arbitrarily-sized arrays, you would need to pass in the memory address of the array itself, and its sizes.

Code:
```....
int twoD[height][width];
//DO SOMETHING
func2(twoD, height, width);
....

void func2(int array[][], height, width)
{
int i, j;
for(i=0; i<height; i++)
{
for(j=0; j<width; j++)
{
printf("%d\n", array[i][j]);
}
}
}```
How can i pass that array? Whats the best way to do that?

10. If you want your func2 to be able to handle any-sized array, the first parameter need to be an int *, and inside you need to handle the arithmetic yourself.

We just did this fairly thoroughly Friday last week, I think; so, as heretical as it may seem, you may need to read someone else's posts to find more information.

11. When you pass a 2D-array, you must specify the size of the outer-most dimensions. I showed with an example before. You had to fill in the width in the array. The height would be optional, but the width is required.
Or pass an int* along with height & size. It will be treated as a 1D array, however.