top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Is there a way to return an array in C from a user defined function?

+2 votes
264 views
Is there a way to return an array in C from a user defined function?
posted Jan 30, 2017 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

well, an array is basically a pointer to an area in memory where a chain of variables of the same data type are stored. so, to answer your question, yes. you can. just return a pointer.

example:
int* functionThatReturnsIntArray(){

//allocating memory for the array dinamically (or you could just receive it as a parameter in your function)
int* array = (int) malloc (sizeof(int) * numberOfElementsInTheArray);

//process the array here

return array;
}

answer Jan 31, 2017 by anonymous
...