Problem Passing Array via pointer

Hey guys,


I've been trying to figure out how to pass arrays via pointers. I must have read a dozen different pages on how to do it, yet when I do it I keep getting weird results. I created a super-short program just to demonstrate my problem:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

using namespace std;
#include <iostream>; 

//This program is for the sole purpose of learning how to pass arrays via pointers

int * createArray();   

int main(){

    int * newList; 
    newList = createArray();
    cout << "List at 0 after being passed is... " << newList[0] << endl;    
    system("PAUSE"); 
    
}

int * createArray(){
    int list[10] = {1, 3, 5, 7, 9, 11, 13, 17};
    int * pList = list;
    cout << "List at 0 before being passed is... " << pList[0] << endl;
    return pList; 
}


When I run this program, I get the following results:


List at 0 before being passed is... 1
List at 0 after being passed is... 4440927


Why does the value change once it is passed? How can I pass this array I'm creating?

PS: I know I could use vectors, static arrays, and other work-arounds. But I'm SPECIFICALLY trying to learn how to pass arrays via pointers.

Thanks a lot guys!

You're passing the pointer correctly; the problem is that the array is local to that function and when the function ends, the machine is free to do whatever it likes to that piece of memory.

int list[10] = {1, 3, 5, 7, 9, 11, 13, 17};

This array you made ceases to exist after the function createArray ends. It is a local variable, on the stack. Passing a pointer to it won't help; the pointer will point at the memory that used to be this array.

You can avoid this by either making the array on the heap (using new, demonstrated below)) so that it exists until you delete it, or you create it in the calling function (i.e. in main) and pass it in.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream> 
using namespace std;
//This program is for the sole purpose of learning how to pass arrays via pointers

int * createArray();   

int main(){

    int * newList; 
    newList = createArray();
    cout << "List at 0 after being passed is... " << newList[0] << endl;    
   
    
}

int * createArray(){
    int * pList = new int[10];

    pList[0] = 76;

    
    cout << "List at 0 before being passed is... " << pList[0] << endl;
    return pList; 
}
Last edited on
Thank you very much for your quick and thoughtful reply!

Would you mind very much showing me an example of the second method you mentioned, of creating the array in main and passing it? I'm having the same difficulty passing a reference to an array INTO a function as I am passing it OUT of a function, so it'd be very useful if I could see it.

Thanks again.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;
//This program is for the sole purpose of learning how to pass arrays via pointers

void fiddleWithArray(int*);   

int main(){

   int list[10] = {1, 3, 5, 7, 9, 11, 13, 17};
   cout << "List at 0 before being passed is... " << list[0] << endl;    
 cout << "List at 1 before being passed is... " << list[1] << endl;    
    fiddleWithArray(list);
    cout << "List at 0 after being passed is... " << list[0] << endl;    
 cout << "List at 1 after being passed is... " << list[1] << endl;    
     
}

void fiddleWithArray(int* input){
    input[0] = 45;
    input[1] = 18;
}
Last edited on
Awesome! Thanks again.
Topic archived. No new replies allowed.