Returning and Passing Arrays Using Points - Getting Junk Values

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.

Here is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>

using namespace std;

void printArray(int* array);
int* createArray();

int main()
{
    printArray(createArray());

    return 1;
}

int* createArray()
{
    int array[10];
    int* pointer;
    pointer = array;
    for(int i=0; i<10; i++){
        array[i] = i;
    }

    return pointer;
}

void printArray(int* array)
{
    for(int i=0; i<10; i++){
            cout << array[i] <<endl;
    }
}


Output:
0
4199366
4618868
3
4
5
6
7
8
9

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

I'm at my limit. Help. Please and thank you :)


Last edited on
closed account (Dy7SLyTq)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>

using namespace std;

void printArray(int* array);
int* createArray();

int main()
{
    printArray(createArray());

    return 1;
}

int* createArray()
{
    int array[10];
    int* pointer;

    for(int i=0; i<10; i++){
        array[i] = i;
    }

    pointer = array;

    return pointer;
}

void printArray(int* array)
{
    for(int i=0; i<10; i++){
            cout << array[i] <<endl;
    }
}
i would try something like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
using namespace std;

void printArray(int myArray[], const int &arraySize); //outputs the contents of an array to the screen
void populateArray(int myArray[], const int &arraySize);  //populates an array with integers

int main()
{
    
	const int 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[], const int &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[], const int &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[], const int &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[], const int &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.
Last edited on
closed account (Dy7SLyTq)
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[], const int &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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
using namespace std;

int* createArray(const int &arraySize);  //creates an array and populates the array with integers
void printArray(int* point, const int &arraySize); //outputs the contents of an array to the screen

int main()
{
    
	const int 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(const int &arraySize)
{
    int* pointer;
	static int 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, const int &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.
Last edited on
In:
1
2
3
4
5
6
7
8
9
10
11
int* createArray()
{
    int array[10];
    int* pointer;
    pointer = array;
    for(int i=0; i<10; i++){
        array[i] = i;
    }

    return pointer;
}


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.
Last edited on
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!
Last edited on
Topic archived. No new replies allowed.