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:
usingnamespace 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.
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.
#include <iostream>
usingnamespace 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 = newint[10];
pList[0] = 76;
cout << "List at 0 before being passed is... " << pList[0] << endl;
return pList;
}
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.
#include <iostream>
usingnamespace 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;
}