get 2 dimensional array from a function to the main

I have 2 dimensional array in a function and I want to get it in my main function
like this way

func()
{
int array[2][3]={2,3,4,5,6,7};
}

int main()
{
/// Iport this 2 dim array
}


thanks for reading
The array inside the function is a local variable. It will be deleted when the control will be pass outside the function. So it is better to declare the array in the main and pass it as argument to the function.
vlad is right, just make sure you indicate the size of the 2nd dimension of your array in your function prototype.

He is assuming, and I am too, that you are not familiar with dynamic data. If you are, then you are able to create within your function a custom-tailored array, and return the address of its first element back to main. More on that, if you are accepting to listen that is.
You can't. You will need to create a declaration of the array in main, or create a global variable (not recommended).

The reason is that int array[2][3] is local to func() and will be destroyed after func() returns.

func() also doesn't have a return value.
Can someone give me some piece of codes ?

un exemple..at least
Last edited on
I hope this example lets you see how a function can pass back to main.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

void func( int array[][3] )
{
    //do stuff with array here, for example add one to everything
    for( int j = 0; j < 2; j++ )
        for( int i = 0; i < 3; i++ )
            array[ j ][ i ]++;
}

int main()
{
    int array[][3] = {{ 1, 2, 3 }, { 4, 5, 6 }};
    func( array );
    //lets check to see if one was added to everything
    for( int j = 0; j < 2; j++ )
        for( int i = 0; i < 3; i++ )
            std::cout << array[ j ][ i ] << " ";
    return 0;
}
Topic archived. No new replies allowed.