Saturday, May 22, 2010

How do I return a 2D array from a function in C?

do i need to use pointers/struct in order to make it easier? If possible, a straightforward and simple way would do... thanks!

How do I return a 2D array from a function in C?
You cannot pass an array to a function by value. A pointer is always passed. You can get the same result as pass-by-value by declaring the array as const. Then the called function cannot change any values in the array.





A contrived by hopefully instructive example.





#include %26lt;stdio.h%26gt;








void cannotChange1(const int arr[], const int size)


{


int i;





for (i = 0; i %26lt; size; ++i)


{


printf("%d\t", arr[i]);


}





printf("\n");


}








void cannotChange2(const int *arr, const int size)


{


int i;





for (i = 0; i %26lt; size; ++i)


{


printf("%d\t", arr[i]);


}





printf("\n");


}








void canChange1(int arr[], const int size)


{


int i;





for (i = 0; i %26lt; size; ++i)


{


arr[i] = arr[i] * 2;


printf("%d\t", arr[i]);


}





printf("\n");


}





void canChange2(int *arr, const int size)


{


int i;





for (i = 0; i %26lt; size; ++i)


{


arr[i] = arr[i] * 2;


printf("%d\t", arr[i]);


}





printf("\n");


}








int* viaReturn(int *arr, const int size)


{


int i;





for (i = 0; i %26lt; size; ++i)


{


arr[i] = arr[i] * 2;


printf("%d\t", arr[i]);


}





printf("\n");








return(arr);


}








int main(int, char **)


{


int a[3] = {1, 2, 3};


int *b;





cannotChange1(a, sizeof(a) / sizeof(int));


cannotChange2(a, sizeof(a) / sizeof(int));





canChange1(a, sizeof(a) / sizeof(int));


canChange2(a, sizeof(a) / sizeof(int));





b = viaReturn(a, sizeof(a) / sizeof(int));





int i;





for (i = 0; i %26lt; sizeof(a) / sizeof(int); ++i)


{


printf("%d\t", b[i]);


}





printf("\n");


}
Reply:You can always create the 2D array off the heap and then pass the address back to the caller and then have the caller use it for whatever but not to forget to free the space when you are done...





Another alternative is to pass in the 2D array as a parameter into the function (by reference) and then filll it and then just leave the function which would maintain the array's content.





You mentioned you are using "C" and not C++ so unfortunately you dont have the luxury of alias nor the new/delete keywords....





I hope i didn't confuse you....because i am not sure you level of what i am talking about maybe i should explain s'more?





It's been a while since i've looked at C but for your 2D array you can pass it back by allocating memory off the systems heap space by using malloc(..) ... my appologies if C uses a different function for this....it's been a while....C++ is more elegant....so in this example:





int *my2DArray;


malloc(my2DArray,sizeof(int)*10);





This will create a 2D Array that can hold 10 integer values.





so here is what your function would look like:





int* MyFunction(void)


{


int *my2DArray = NULL;


malloc(my2DArray,sizeof(int)*10);





/* Do stuff Here */





return my2DArray;


}








void main()


{


int *myArray=NULL;


myArray = MyFunction();





/* Do whatever here */





free(myArray);





}


No comments:

Post a Comment