0
int* create_random_array(int n){
    int arr[n];

    srand(0);
    for (int i = 0; i < n; i++)
    {
        // Fill with random ints 1-100
        arr[i] = (rand() % 100)+1; 
    }
    return arr;
}

int main( void ){
    int* create_random_array(int n);
    int *arr[10];
    *arr = create_random_array(10);

    for (int i = 0; i < 10; i++)
    {
        printf("%d\n",arr[i]);
    }
}

I am very new to C and programming in general. I am just struggling to understand how to print the array created from a function with pointers.

Here is what I am trying to print:

4
5
2
3
98
12
23
12
23
23

Instead I am printing:

1346144960
1346145216
260272128
1708622408
1346145232
0
0
0
0
0

Any hep would be greatly appreciated, thank you in advance.

0

I think the better approach is to make that a void function that takes two arguments, the array and the number of elements.

void create_random_array(int *arr, int n)
{
    srand(0);
    for (int i = 0; i < n; i++)
    {
        // Fill with random ints 1-100
        arr[i] = (rand() % 100)+1; 
    }
}

And then, in main, you would simply call create_random_array(arr, 10); after declaring int arr[10];

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .