C function returns an array
I am writing a method that takes a number l and returns a vector of size l with random numbers. I have this code but it doesn't work
#include <time.h>
int makea (int z) {
int a1[z];
int i;
for (i = 0; i < tam; i++) {
a1[i]=srand(time(0));
}
return a1;
}
These are the errors that the compiler returns
arrays1.c: In function 'makea':
arrays1.c: 12: error: void value is not ignored as it should be arrays1.c: 14: warning: return makes an integer from a pointer without translating
arrays1.c: 14: warning : the function returns the address of a local variable
I think this is a pointers problem ... but I'm not sure.
source to share
Several problems:
- Your array is allocated on the stack, which means that when your function completes, the returned memory will be invalid.
- In C, you cannot return an array from a function, it must expand the pointer first.
So, to fix, use malloc and a pointer:
int *makea (int z) {
int *a1 = malloc(sizeof(int) * z);
int i;
srand(time(NULL));
for (i = 0; i < tam; i++) {
a1[i]= rand();
}
// remember to free a1 when you are done!
return a1;
}
Also note that using malloc can sometimes in principle provide you with a "random number" scenario for free, negating the need to cycle through the elements, since the value returned from malloc is garbage (and thus random numbers).
However, also note that malloc is implementation specific, which means that the implementation can in theory clear the memory for you before returning it.
source to share
Your best bet:
-
Declare the array outside of the procedure and pass it for initialization:
void init_array (int a[], nelms)
-
Plan B passes pointer to pointer and assigns and initializes its subroutine
Like this:
void alloc_and_init_array (int **a_pp, int nelms)
{
*a_pp = malloc (sizeof (int) * nelms);
...
... or equivalently ...
int *
alloc_and_init_array (int nelms)
{
int *a_p = malloc (sizeof (int) * nelms);
...
return a_p;
source to share
Okay, first your function says it returns an int, but you want to return an array, so this is wrong. Of course, you cannot return an array in C either ...
Second, you will need to return a pointer. You cannot copy arrays using assignment, or assign a new value to an array at all in C, so your function will not be very useful. Either return int*
or take int**
as an output argument and initialize it in your function.
Also, your array is locally allocated, so even if the compiler didn't complain, you are returning invalid memory.
int makea (int size, int **out_array) {
int *temp, i;
if(!out_array)
return 0;
temp = malloc(sizeof(int) * size);
if(!temp)
return 0;
srand(time(0));
for (i = 0; i < size; ++i)
temp[i] = rand();
*out_array = temp;
return 1;
}
int main() {
int *arr;
if(!makea(10, &arr)) {
printf("Failed to allocate array");
return -1;
}
return 0
}
Another note:
temp[i] = srand(time(0));
It is not right. srand
places a random number generator, but does not return a random number. You call srand
to inject the seed and then you call rand
to get a random number.
source to share