3
Contributors
4
Replies
5
Views
8 Years
Discussion Span
Last Post by Luckychap
0
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();
}
0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.