0

I'm trying to write a program that calls upon a function that creates an array with random numbers and then sort them. When thats done I want to return a pointer to the array to the main function. I then want to print the array. And everything works fine if I just print one value in the array. However if I print more than one value I get some other value. Here is the main function:

int * createArr(){
    int arrLenth = 100;
    int arr[arrLenth];
    int i, j, temp, pos = 0;

    /*ASSIGNING ARRAY VALUES */
    for(i=0;i<arrLenth; i++){
        arr[i] = rand() % 900;
    }
    /* SORTING ARRAY */
    for(i = 0; i < arrLenth; i++){
        for(j = 0; j < arrLenth; j++){
            if(arr[j]>arr[j+1]){ 
                temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
            }
        }
    }
    /* PRINTING ARRAY*/
    for(i=0;i<arrLenth; i++){
        printf("%d, ", arr[i]);

    }
    int *parr = arr;
    //printf("\n%p", parr);
    return parr;
}
    int main() {
        int *p = createArr(); //pointer to the array

        printf("\n%d", *(p+1));
        printf("\n%d", *(p+1));

        return 0;
    }

This gives me the output:

45 (which is correct)
1771768448 (which is not correct)

What is causing this? I'm printing the exact same line twice but get difrent outputs. Also tell me if you need to see the function where i create the array, I don't feel like it's needed because everything works fine there.

WilliamG
  • 451
  • 1
  • 8
  • 16
  • 2
    Using my crystal ball: you're returning a pointer to a local variable from createArr. – Mat Nov 18 '17 at 11:47
  • 1
    Of course you need to show the function where "everything is working fine". Without seeing your function, though, I'd say that you are probably creating your array on the stack. Also the second print is probably of `*(p+2)`, or the first one prints `*(p+0)`. – Sergey Kalinichenko Nov 18 '17 at 11:48
  • 3
    Please show us a [Minimal, **Complete**, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Nov 18 '17 at 11:48
  • And unrelated to your problem, but for any pointer or array `p` and index `i`, the expression `*(p + i)` is exactly equal to `p[i]`. Using `p[i]` is usually easier to read and understand, and also less to write. :) – Some programmer dude Nov 18 '17 at 11:49
  • Post your code for createArr, otherwise we cannot guess – SwiftMango Nov 18 '17 at 11:49
  • Ok, I have added createArr – WilliamG Nov 18 '17 at 11:54
  • 2
    And it is as people guess. You return a pointer to the first element of the array `arr`, an array whose scope and lifetime ends when the function returns. – Some programmer dude Nov 18 '17 at 11:57
  • Thanks, but I can't see how that's the problem when I can print any single value of the array from the main function?? – WilliamG Nov 18 '17 at 11:59
  • 1
    It's a problem because the array doesn't really exist in memory any more. The memory itself is not removed or explicitly overwritten, but the variables inside a function that returns cease to exists and you should just not attempt to access them again. Trying to dereference a pointer to such variables leads to [*undefined behavior*](https://en.wikipedia.org/wiki/Undefined_behavior). – Some programmer dude Nov 18 '17 at 12:09
  • 1
    Make the array global and you should be fine – Lok Nov 18 '17 at 12:13
  • Okay thank you! lok Why would I make it global if i want to practice pointers? – WilliamG Nov 18 '17 at 12:14
  • 1
    @william - Here is a (really funny) explanation of why *some* values *sometimes* will print correctly. [Can a local variable's memory be accessed outside its scope?](https://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope) – Bo Persson Nov 18 '17 at 12:53
  • I ran the same code you gave in question and it's working fine. You can try making the parr variable global (declare it outside the main and the other function) – H. Sodi Nov 18 '17 at 14:30

0 Answers0