what is the syntax of returning a two dimensional to main function?

Recommended Answers

All 4 Replies

int** foo()
{
    int i;
    int** array = malloc(100 * sizeof(int *));
    for(i = 0; i < 100; i++)
        array[i] = malloc(20 * sizeof(int)); // allocate 20 integers
    return array;
}

int main()
{
    int ** array = foo();
}

for simplification returned array will be of type:

int array[100][20] ;

or u can modify above function to get variable 2D array:

int** foo(int row, int col)
{
    int i;
    int** array = malloc(row * sizeof(int *));
    for(i = 0; i < 100; i++)
        array[i] = malloc(col * sizeof(int)); // allocate 20 integers
    return array;
}

int main()
{
    int ** array = foo(100,20);
}

same result.

Luckychap;
ur solution is realy helpful. Thak you so much...

Correction:

for(i = 0; i < row; i++)
commented: 2 months dead, and you bring it up for a typo? -2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.