0

Return whole array

Can someone tell how i can return a whole array from a function to my main function? And can i do arr[] = arr2[];?

24th Oct 2019, 8:43 AM
Julian Dörling
Julian Dörling - avatar
2 Answers
+ 1
And you have the option not to return an array, but just hand over its address to the function and fill it right there: int do_stuff_with_array(int *array, int size) { // And here you do with it whatever you need }
24th Oct 2019, 12:51 PM
HonFu
HonFu - avatar
+ 3
Sers! This is where it gets a bit complicated. You want to check the tutorial sections about pointers and dynamic memory. Returning an array is not possible in C, but returning a pointer is: int* make_array () { int* a = malloc(4 * sizeof(int)); return a; } int main () { int* p = make_array(); p[3] = 1234; free(p); } Note how we're dealing with pointers here but at the end I'm still using `p` like an array. That's because in C, there is no difference between a pointer and an array. If you have any questions just let me know.
24th Oct 2019, 9:08 AM
Schindlabua
Schindlabua - avatar