0

I'm trying to create a function that creates an array in size n with random numbers stored inside, I've been trying several option but once i use the array in main, only the first number shows up correctly.

I'm using CodeBlock by the way.

static int *arr;
int i, num;
printf("enter the length of the array: ");
scanf("%d/n", &num);
arr = get_random_arr(num);

for(i=0;i<num;i++)
{
    printf("outside the function: %d\n", *(arr+i));
}

return 0;

int *get_random_arr(int num)
{
    int temp_arr[num];
    int i;
    srand((unsigned)time(NULL));

    for (i=0;i<num;i++)
    {
        temp_arr[i] = rand() % 1001 ;
        printf("inside the function: %d\n",temp_arr[i]);
    }

    return temp_arr;
}

and this is the code that compiles:

enter the length of the array: 3
inside the function: 224
inside the function: 774
inside the function: 60
outside the function: 224
outside the function: 2686648
outside the function: 1977872892

Thank's to "coderredoc" his answer was the solution i was looking for

R.K
  • 108
  • 5
  • 1
    Your array is locally scoped to your function. Once you leave that scope, any pointers to the array are garbage. You'll want to allocate it on the heap if you want to access it after you leave the scope of the function. – Christian Gibbons Dec 07 '17 at 17:06
  • you should not return local array to a function. scope problem ? – Achal Dec 07 '17 at 17:06
  • Two options: (1): Declare the function as `void get_random_arr(int num, int *arr)` and make the caller responsible for allocating the array and passing a pointer to `get_random_arr`. Or (2): Replace `int temp_arr[num];` with `int *int_arr = calloc(num, sizeof(*arr));` in `get_random_arr`to allocate the array (actually just a block of memory) on the heap and return that. The caller will be responsible for calling `free` to free the memory to avoid memory leaks. – Ian Abbott Dec 07 '17 at 17:23
  • 1
    While you're at it, take `srand()` out of your function and put it in `main()` where it belongs. – Lee Daniel Crocker Dec 07 '17 at 18:38

2 Answers2

1
scanf("%d/n", &num);

You have to enter the number followed by / and n. This is not what you want. You wanted this

scanf("%d\n", &num);

But here the scanf reads the number in num but then the reading continues until nonwhitespace character followed by \n is found. You don't need the \n here.

Also returning return temp_arr; and using it outside it's scope is UB.

The array temp_arr has automatic storage duration meaning its lifetime will end after the function ends. Accessing it outside its scope is undefined behavior which is precisely what you did.

Something like this

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int *get_random_arr(int num)
{
    if( num <= 0){
        fprintf(stderr, "%s\n", "Error in the number: num >= 1");
        exit(1);
    }
    int *temp_arr = (int*)malloc( sizeof *temp_arr *num);
    if( !temp_arr ){
        fprintf(stderr, "%s\n", "Error in malloc");
        exit(1);
    }

    srand((unsigned)time(NULL));

    for(size_t i=0;i<num;i++)
    {
        temp_arr[i] = rand() % 1001 ;
        printf("inside the function: %d\n",temp_arr[i]);
    }
    return temp_arr;
}
int main(){
    static int *arr;
    int num;
    printf("enter the length of the array: ");
    if( scanf("%d", &num) != 1){
        fprintf(stderr, "%s\n","Error in input" );
        exit(1);
    }
    arr = get_random_arr(num);

    for(size_t i=0;i<num;i++)
    {
        printf("outside the function: %d\n", *(arr+i));
    }
    free(arr);

    return 0;
}

The code tries to show a possible way to achieve what you wanted. Dynamically allocated memories lifetimes extends beyond the scope of the function. That's why it works. Note the free() - which is used to free the dynamically allocated memory.

user2736738
  • 28,590
  • 4
  • 37
  • 52
0

The first and foremost, in your code, temp_arr is local to the function get_random_arr(). Once the function finishes, the returned address is not valid anymore!

The moment you try to use the returned value, it causes undefined behavior, as the accessed memory is invalid in the context of the caller.

That said, not checking the return value of scanf() also posses a problem. In case scanf() fails, you'll be end up using an unitialized value.

Sourav Ghosh
  • 127,934
  • 16
  • 167
  • 234