can anyone explain how can a function return a double pointer with which a 2 dimensional array can be traversed, this function takes the number of rows and columns as arguments:
like:

``````void ** function(int row, int col)
{
void **returningPointer= NULL;
void*p;

for (i = 0; i < row; i++)
{
p[i] = malloc(szeof(int));
}
}
``````

## All 6 Replies

Why would you want to make it void?

``````double** foo(int rows, int cols)
{
double ** ptr = malloc(rows * sizeof(double *));
<snip>
return ptr;
}``````

thanks for this...

but won't it be difficult to traverse a 2 dimensional array with a single pointer, won't we require a array of pointers which wud point at each sudsequent row and then a double pointer to the array of pointers....

help me with its coding part...
something like this

``````void ** function(int row, int col);
//This is the declaration part``````

Are you asking another question? Your post is confusing because you put a question in quote tags.

>>won't it be difficult to traverse a 2 dimensional array with a single pointe

You mean like treat `double* ptr;` as if it were `double ** ptr;` ? If you want row 1 column 2 then its just `ptr[ (row-1) * ColsPerRow + col];` If course you will have to make sure the value os row is > 0 before making that subtraction.

Actually i want this function to return a void double pointer like

``void ** ptr``

which we can later type cast to any data type, and with this we can traverse our 2 dimensional array...

In c language you can't write a function that returns a pointer of some unknown type then typecast it to whatever you want. malloc() works like that because you tell malloc() how many bytes of data to allocate, and it gives you back a pointer to that block of memory. But the function you propose doesn't work like that. The parameters to your function are row and col, and the function doesn't know the critical piece of information -- size of each item. It has to know the data type (or size) of each element in the array.

you could write a function like this

``````void ** foo(int rows, int cols, int itemSize)
{
char** ptr = malloc(rows * sizeof(char *));
for(i = 0; i < rows; i++)
ptr[i] = malloc(cols * itemSize);
return ptr;
}``````

>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.

Be a part of the DaniWeb community

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