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.

+3


source to share


5 answers


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.

+6


source


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;

      

+1


source


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.

0


source


A local variable like your array is allocated on the stack. When the function returns, it is popped off the stack, so the returned pointer points to an unallocated memory location.

You have to allocate the array using malloc () or pass an already existing array to a function.

0


source


#include <time.h>    

int makea (int z) {
    int *a1 = (int*)malloc(z*sizeof(int));
    int i;

    for (i = 0; i < tam; i++) {
        a1[i]=srand(time(0));
    }

    return a1;
}

      

IMPORTANT: Remember to free memory somewhere outside when you no longer need it.

0


source







All Articles