# How do I create 2 dimensional array in function and use outside of function ?

• 11-07-2001
Cahunna54
How do I create 2 dimensional array in function and use outside of function ?
I have a program to re-write for a small amount of extra credit, but have not been able to find examples/info in books or on the web.
Originally the program involved three arrays with 2 dimensions each. Elements of arrays 1 and 2 were added together to create the elements for array three. That went okay I think, I got the desired results etc.
Now I need to create two functions to be called from main, that will create/fill the arrays and send back the array data to main to do the calculations for the third array.
I thought my function prototype, call etc was set up properly, but I either get zero's in everything or a few systax or declaration not terminated properly errors. I have not been able to locate examples of the right way to set up these functions.
Don't laugh, but this is one of many variations I have tried:
Prototype
int even();
Call
int even ()
Body
int(arrE[2][4])even():confused:
• 11-07-2001
Salem
Erm, I think the arrays stay in main, and you pass these arrays (as pointers) to your functions (which will update the arrays in main, because arrays are passed as pointers)

For a [2][4] array, it goes like this
Code:

```void init ( int arr[2][4] ); int main ( ) {     int foo[2][4];     init( foo );     return 0; } void init ( int arr[2][4] ) {     // do stuff }```
• 11-07-2001
Cahunna54
I think I don't understand arrays in functions
This is what the mess looks like
/* A Program to populate 2 arrays, one with even numbers and one with odd numbers
then use an empty array to store the totals of the numbers in the two original
arrays. Print/display the contents of all three arrays.
Populate the arrays by using functions
Version 2, Function Usage */

#include <stdio.h>

void even(int arrE[2][4]); /*Function Prototype*/
void odd(int arrO[2][4]); /*Function Prototype*/
int arrE [2][4];/*Array Variable*/
int arrO [2][4];/*Array Variable*/
int arrTotal[2][4]; /*intialize the array to hold the totals of the other arrays*/
int r,c;

int main ()
{

even (int arrE[2][4]);
odd (int arrO[2][4]);
for (r=0;r<2;++r) /*for loop, for beginning row, max rows and increment*/
{
for (c=0;c<4;++c) /*for loop total #'s, for beginning column, max columns, and increment*/
{
arrTotal[r][c]=(arrE[r][c]+arrO[r][c]); /*formula to add elements of two arrays
and place in elements of total array*/
printf("\nThese are the totals of the elements : %d plus %d equals %d",
arrE[r][c],arrO[r][c],arrTotal[r][c]); /*Display results*/
}
}
printf("\n\nThe program has ended !!!"); /*Message*/

return;
}

void even(int arrE[2][4])
{

int arrE [2][4]={{2,4,6,8},{10,12,14,16}};/*Populate the even # array*/

for (r=0; r<2; ++r) /*for loop even #'s, for beginning row, max rows and increment*/
{
for (c=0;c<4;++c) /*for loop, for beginning column, max columns, and increment*/
{
printf("\t%d ",(arrE[r][c])); /*display array elements*/
}
}
return ();
}

void odd(int arrE[2][4])
{
int arrO [2][4]={{1,3,5,7},{9,11,13,15}}; /*Populate the odd # array*/
for (r=0; r<2; ++r) /*for loop even #'s, for beginning row, max rows and increment*/
{
for (c=0;c<4;++c) /*for loop, for beginning column, max columns, and increment*/
{
printf("\t%d ",(arrE[r][c])); /*display array elements*/
}
}
return ();
}
• 11-07-2001
Salem
Getting close...

> even (int arrE[2][4]);
> odd (int arrO[2][4]);
When calling functions, its just the array name
even ( arrE );
odd ( arrO );

And your even function should look like this
Code:

```void even(int arrE[2][4]) {     int temp [2][4]={{2,4,6,8},{10,12,14,16}};/*Populate the even # array*/     int r, c;     for (r=0; r<2; ++r)  /*for loop even #'s, for beginning row, max rows and increment*/     {         for (c=0;c<4;++c) /*for loop, for beginning column, max columns, and increment*/         {             arrE[r][c] = temp[r][c];         }     }     return; }```
Something similar for odd...
• 11-07-2001
Cahunna54
whew !!!!!
thx so much, I shall study the errors in my code.
A few changes and it Works !!!!!!!!