twbbas
twbbas

Reputation: 447

C program: Integer array pointer changes values when passed as parameter

My problem is that my integer array's values change when it is passed to the function calculate. The values are correct for indexes 0, and 2->5.

For some reason, indexes 1 and 6+ are not the correct values.

Below is my code.

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int* generate_rand (int length, int MAX_ARRAY);
void calculate (int *array_ptr, int length, int *mean, int *sd);

main() {
    srand(time(NULL));
    int a;
    printf("\nArray length?: ");
    scanf("%d", &a);
    int* array_ptr2;
    array_ptr2 = generate_rand(a, 100);
    //int mean, sd;
    int* *mean;
    int* *sd;
    int i = 0;
    for (i = 0; i < 10; i++) {
        printf("Array2: %d\n", *(array_ptr2 + i));
    }
    calculate(array_ptr2, a, *mean, *sd);
    //printf("Mean: %d\n", (int)*mean);
}

int* generate_rand (int length, int MAX_ARRAY) {
    int arr[length];
    int i;
    for (i = 0; i < 10; i++) {
        int r = rand()%MAX_ARRAY;
        arr[i] = r;
        printf("Rand: %d\n", arr[i]);
    }
    int *arrPtr;
    arrPtr = &arr[0];
    return arrPtr;
}

void calculate (int *array_ptr, int length, int *mean, int *sd) {
    int sum;
    int i;
    for (i = 0; i < length; i++) {
        printf("Array: %d\n", *(array_ptr + i));
        sum += *(array_ptr + i);
        //array_ptr++;
        printf("Sum: %d, i:%d\n", sum, i);
    }
    //*mean = sum / length;
}

Do you know what I'm doing wrong?

Upvotes: 1

Views: 3597

Answers (3)

Arun
Arun

Reputation: 20383

generate_rand() is returning a pointer to arr[0] where arr is a local variable. The local variable goes out of scope as soon as the function, generate_rand(), returns. Once a variable is out of scope, there is no guarantee about its value, in fact, accessing an out of scope of variable is undefined.

One possible solution: generate_rand() can allocate the array arr on the heap, i.e. allocate memory using malloc(). Needless to say, the memory must be free()-ed as soon as it is no longer necessary.

Upvotes: 3

Code-Apprentice
Code-Apprentice

Reputation: 83527

As you state in the comments, generate_rand() needs to return a pointer to an array that is filled with random values. At the moment, you are returning a pointer to the first element of a local array. As other's have stated, the memory of this array is only guaranteed to be available to your program inside of the generate_rand() function. In order to create an array which you can use anywhere in your program, you need to allocate the memory with malloc(). I suggest you research memory management further. You need to learn about both the malloc() and free() functions.

Upvotes: 1

dreamlax
dreamlax

Reputation: 95335

The problem is that you are returning the address of a local variable of a function. Local variables of functions cease to exist after a function has exited, so the returned pointer suddenly becomes dangling (as in, not pointing to anything useful).

You need to allocate memory for your array with malloc, because the memory returned from malloc will exist until it is freed with free (which you can call anytime afterwards).

int* generate_rand (int length, int MAX_ARRAY) {
    int *arr = malloc(sizeof(int) * length);

    int i;

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

    return arr;
}

When you no longer need the array returned from this function, pass it to free to release the memory, e.g.:

int *arr = generate_rand(10, 100);
// do something with arr here...
free(arr);

Upvotes: 3

Related Questions