1

I want to write a function in C language which returns the 2 dimension array to my main function.

0

4 Answers 4

4

A function that returns a dynamically allocated two dimensional array for the int type.

int ** myfunc(size_t width, size_t height) 
{
    return malloc(width * height * sizeof(int));
}

It has to be deleted with free once it is not used anymore.

EDIT This only allocates the space for the array which could be accessed by pointer arithmetic but unfortunately not by the index operators like array[x][y].

If you want something like this here is a good explanation. It boils down to something like this:

int ** myfunc(size_t ncolumns, size_t nrows)
{
    int **array;
    array = malloc(nrows * sizeof(int *));
    for(i = 0; i < nrows; i++)
    {
        array[i] = malloc(ncolumns * sizeof(int));
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

You can't return int ** if you allocated like this. int ** is an array of pointers. If you do int **p=myfunc(xsize,ysize), and try to access p[x][y], you will crash.
@ugoren: You are correct, only the size of the array is allocated and the use of array[x][y] would crash the program. I'll update my answer.
@Constantinius Thanks for the reply...But 1 thing i m still not cleared is about the syntex of return..Is it possible to return it like-- Return array; and while calling the function should be done like-- int m[][]=myfunc(int a,int b); .Please help me??Is It Right?
@shweta: that is correct. You could also write int ** which is essentially the same thing.
4

You can not simply return malloc( x * y * sizeof(type) );

This will produce a segmentation fault when you try to index the array as a 2 dimensional array.

int **a = malloc(10 * 10 * sizeof(int));

a[1][0] = 123;
a[0][1] = 321;

The result of the above: Segmentation fault

You need to first create an array of pointers with c/malloc() then iterate this array of pointers and allocate space for the width for each of them.

void *calloc2d(size_t x, size_t y, size_t elem_size) {
    void **p = calloc(y, sizeof(void*));
    if(!p) return NULL;

    for(int i = 0; i < y; i++) {
        p[i] = calloc(x, elem_size);
        if(!p[i]) {
            free2d(y, p);
            return NULL;
        }
    }

    return (void*)p;
}

And free

void free2d(size_t y, void *matrix) {
    if(!matrix) return;

    void **m = matrix;
    for(int i = 0; i < y; i++) {
        if(m[i]) free(m[i]);
    }
    free(m);

    return;
}

4 Comments

This is the only correct answer (so far), and the only one downvoted... But it's perhaps too complicated - this code is right if you want to get int **, but you don't have to use an array of pointers to get a 2D matrix.
Hey, thanks for the support :). My personal favorite, which as far as I know isn't possible to return however, is: int (*matrix)[width] = malloc( width * height * sizeof(int));
It's possible to return. see my answer
It's not for arbitrarily sized matrices, also, you would need to create one separate function for all sizes you could possibly need. The pointer you assign the return value to must know of the size. My point is, you can not return the width information with a naked pointer.
1

It should be simple i guess...

char **functioName(char param[][20])
{
  char[][20] temp = malloc(20 * 20 * sizeof char);


 //processing  
  return temp

}

Comments

1

If you know the size of the array, you can do so:

int (*myfunc())[10] {
 int (*ret)[10]=malloc(10*10*sizeof(int));
 return ret;
}

This function return pointer to 10 arrays of 10 ints.

Of course, you should use it as it is, and don't forget to free it after using.

1 Comment

what if 10 is a variable, say N?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.