Here's my solution.
int** allocateMatrix(int m, int n)
{
int **rowList = (int**)malloc(sizeof(int)*m*n + sizeof(int*)*m);
int *colList = (int*)(rowList+m);
int i;
for(i=0; i<m; i++)
{
rowList[i] = colList+i*n;
}
return rowList;
}
And here's the main method to test/understand the allocation.
int main()
{
int m=3, n=4;
int **mat = allocateMatrix(m,n);
int i, j, c=0;
int wordCount= m*n+m; //sizeof(int)*4*5 + sizeof(int*)*4; counting
words so sizeof is not needed
int* memMap = (int*)mat;
// Fill array elements with some values to be able to test
for(i=0; i<m; i++)
for(j=0; j<n; j++)
mat[i][j] = c++;
printf("\nAddress\t Value\n");
for(i=0; i<wordCount; i++)
printf("\n%u\t==> %u", memMap+i, memMap[i]);
getchar();
}
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.