Adobe Interview Question
Software Engineer / Developers// Access elements as myarray[i][j]
void Alloc2D(int no_of_rows, int no_of_columns)
{
int **myarray = (int **)malloc(no_of_rows * sizeof(int *));
for(int i = 0; i < no_of_rows; i++)
{
myarray[i] = (int *)malloc(no_of_columns * sizeof(int));
}
}
//Method2 (keep the array's contents contiguous)
// Access elements as myarray[i][j]
void Alloc2D_2(int no_of_rows, int no_of_columns)
{
int **myarray = (int **)malloc(no_of_rows * sizeof(int *));
myarray[0] = (int *)malloc(no_of_rows * no_of_columns * sizeof(int));
for(int i = 1; i < no_of_rows; i++)
myarray[i] = myarray[0] + (i * no_of_columns);
}
A good explanation here
- Jack December 30, 2014campuscoke.blogspot.in/2014/12/allocate-two-dimensional-array-using.html