>which we can later type cast to any data type, and with
>this we can traverse our 2 dimensional array...
Um, no. void** doesn't have the same generic properties as void*. Rather than a pointer to a pointer to an unspecified type, void** really is a pointer to void*. Which means you can't do this if function returns void**:
double **p = function( 10, 10 );
void** and double** are incompatible, whereas void* and double** would be. You want your function to return void* to get the generic pointer type, which can then be converted back to the original type:
#include <stdio.h>
#include <stdlib.h>
void *function(int row, int col)
{
int **p = malloc ( row * sizeof *p );
int i;
for ( i = 0; i < row; i++ )
p[i] = malloc( col * sizeof *p[i] );
return p;
}
int main ( void )
{
int **p = function ( 5, 5 );
int i, j;
for ( i = 0; i < 5; i++ ) {
for ( j = 0; j < 5; j++ )
p[i][j] = i + j;
}
for ( i = 0; i < 5; i++ ) {
for ( j = 0; j < 5; j++ )
printf ( "%-3d", p[i][j] );
puts ( "" );
free ( p[i] );
}
free ( p );
return 0;
}
Confusing? You bet, but that's how it is.