Hello and thanks! I am having trouble with a simple code that creates an array of 10 elements and prints it.
I have a function that returns a pointer to an array, and a function that accepts a pointer to an array and prints those values.
The problem is that I get really weird values, but then I see some values that are correct.
As you can already see, I did research and found that arrays cannot be returned nor can they be accepted as parameters. They must be returned and accepted as a pointer. And I thought I nailed it. But obviously something is wrong.
And when I increase the array size to 20, my output becomes even weirder:
0
0
2686728
4494478
4683872
5
6
2130567168
0
0
(3 more junk values)
13
14
15
16
17
18
19
#include <iostream>
usingnamespace std;
void printArray(int myArray[], constint &arraySize); //outputs the contents of an array to the screen
void populateArray(int myArray[], constint &arraySize); //populates an array with integers
int main()
{
constint SIZE = 10;
int myArray[SIZE];
populateArray(myArray, SIZE);
printArray(myArray, SIZE);
//stops the program so the user can see the results
cin.ignore(1000, '\n');
return 0;
}
//This functin populates an array with integers
void populateArray(int myArray[], constint &arraySize)
{
for(int i=0; i< arraySize; i++)
{
myArray[i] = i;
}
}
//This function outputs the contents of an array to the screen
void printArray(int myArray[], constint &arraySize)
{
for(int i=0; i<arraySize; i++)
{
cout << myArray[i] <<endl;
}
}
That code worked Yanson. I appreciate it. But just for the purposes of good software engineering, I did not want to have to create the array in my main method. Either way, it is the only way that I can get to work. I appreciate it a lot. One question (if you see this). Why is there an '&' sign before the constant int arraySize?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
//This functin populates an array with integers
void populateArray(int myArray[], constint &arraySize)
{
for(int i=0; i< arraySize; i++)
{
myArray[i] = i;
}
}
//This function outputs the contents of an array to the screen
void printArray(int myArray[], constint &arraySize)
{
for(int i=0; i<arraySize; i++)
{
cout << myArray[i] <<endl;
}
}
I took it out and it worked fine. And from a software engineering perspective, why make it editable?
And thanks for the reply DTSCode. I got the same problem but I appreciate the attempt.
i thought it probably wouldnt work. thats a lot of memory manipulation that could go wrong. the & in that case is (almost) useless. when you put that in front of a variable in a function argument, it passes the actual variable instead of a copy, allowing to manipulate it.
side note: use vector instead of array
The & isn't needed i put it there for efficiency. It is more efficient to pass by referance than it is to pass by value. If you use this void printArray(int myArray[], int arraySize);
when the function is called the compiler has to make a copy of the input argument SIZE. Making a copy takes some time. You can avoid this overhead by passing the argument by referance.
when you use this void printArray(int myArray[], constint &arraySize);
the address of SIZE is passed to the function. the compiler dosn't have to make a copy so the program runs a little faster. The const (constant) makes it impossible to change the value of arraySize. So there is no danger of accidentily changing its value. The performance gains in a program like this one is minimal, so it could be omitted but in larger programs with many functions this could be more inportant.
I believe the code in my original post is the way to go here. However, i thought about how I might go about creating an array in a function and passing it to another function using pointers. This is what i came up with.
#include <iostream>
usingnamespace std;
int* createArray(constint &arraySize); //creates an array and populates the array with integers
void printArray(int* point, constint &arraySize); //outputs the contents of an array to the screen
int main()
{
constint SIZE = 10;
int* myPointer;
myPointer = createArray(SIZE);
printArray(myPointer, SIZE);
//stops the program so the user can see the results
cin.ignore(1000, '\n');
return 0;
}
//This functin populates an array with integers
int* createArray(constint &arraySize)
{
int* pointer;
staticint myArray[10]; //editid this after seeing cire's comment
pointer = myArray; // thanks for the input cire
for(int i=0; i< arraySize; i++)
{
*(pointer + i ) = i;
}
return pointer;
}
//This function outputs the contents of an array to the screen
void printArray(int* point, constint &arraySize)
{
for(int i=0; i<arraySize; i++)
{
cout << *(point + i) <<endl;
}
}
i've never tried this before so there might be a better way to do it.
array is a local-to-CreateArray variable that stops existing as soon as CreateArray returns. So, what you are actually doing here is returning a pointer to an array that no longer exists. Dereferencing that pointer results in undefined behavior.
Cire, that is very smart. And I think that is right.
If there is any doubt in my mind, it stems from the fact that half of the array still printed out for some reason or another.
But the original premise you made still remains. I should never have attempted to create the array in a function in the first place.
Thank you very much!