Function that returns array?

Does anybody know how one goes about having a function that returns a number of values. This is something I have been stuck on for a while and I was wondering if somebody can lead me in the right direction. I thought this could work but had no luck.

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
  int *NumArray(int arr[], const int s);
const int size = 5;

int main()
{
	int values[size];
	int *show = 0;
	cout << "Enter Values: " << endl;
	*NumArray(values, size);
	show = NumArray(values, size);
	cout << "Values: " << *show << endl;
	system("Pause");
	return 0;
}

int *NumArray(int arr[], int s)
{
	
	int * out;

	for(int i = 0; i < s; i++)
	{
        cin >> arr[i];
		out = &arr[i];
	}

	return out;
}


Thanks in advance.
That would work. You need to create an array to return though.
1
2
3
4
5
6
int *NumArray(int arr[], int s)
{
   int * out = new int[s];
   //do something to fill out
   return out;
}

And whenever you are done with this new memory remember call delete[] on it.

I notice that you are trying to cout the pointer that is returned in main. If you want to output each of the numbers, you will need to loop over the array to cout everything.
Last edited on
I thought int arr[] is seen as int* arr by the compiler anyway? If so there's no need to create a new array in the function to return a pointer to it since you'll be working with the original array anyway. No need to return anything either. That's if my memory serves. Been a while since I've done something like this.
@kevinkjt2000, thanks for the reply. Can I ask why you chose to make the int arr[] variable a dynamic array?

Also, I seem to have a problem with only getting 5 inputs from the user. For example, when I try to enter my 5 values this happens: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10. Then the program outputs

6
7
8
9
10

When the intended values were
output:


1
2
3
4
5

Here is the fixed 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
int *NumArray(int arr[], const int s);
const int size = 5;

int main()
{
	int values[size];
	int *show = 0;
	cout << "Enter Values: " << endl;
	*NumArray(values, size);
	show = NumArray(values, size);
	for(int i = 0; i < size; i++)
	{
	 cout << "Values: " << values[i] << endl;
	}
	
	system("Pause");
	return 0;
}

int *NumArray(int arr[], int s)
{
	
	int * out= new int [s];

	for(int i = 0; i < s; i++)
	{
        cin >> arr[i];
		out = &arr[i];
	}
   
	return out;
   delete [] out;
}


Last edited on
You are overwriting the memory address stored to out!
Every time you do out = you are assigning a new value to the variable out.
Inside your for loop you are overwriting the value that was originally given by the new operator when you dynamically created an array.
Instead change line 27 cin >> arr[i]; to cin >> out[i]; and remove line 28 out = &arr[i];
Line 32 delete [] out; should be removed. This line would never be executed anyways, since it is right after a return statement. What I meant was that somewhere in main you should call delete[] after you are finished with the array that is returned. I also noticed on line 13 cout << "Values: " << values[i] << endl; you used values[i] and you probably meant show[i], since show is what you call the array returned by NumArray.

@Raezzor: I recommended new because he was creating a new variable that had nothing assigned to it. So to me it seemed like he wanted a brand new array to work with? But I am just assuming... So hopefully the OP will clarify this. It is unnecessary to have a function to have an array passed as a parameter and do nothing with it.
Last edited on
I was wondering if somebody can lead me in the right direction

The right direction is to return a vector (or, if your sizes are known at compile time, a C++ array such as std::array<int, size> in your case)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <vector>

const int size = 5;

std::vector<int> NumArray(int s)
{
    std::vector<int> out;
    for(int n, i = 0; i < s; ++i)
        if(std::cin >> n)
            out.push_back(n);
    return out;
}

int main()
{
    std::cout << "Enter Values: \n";
    std::vector<int> values = NumArray(size);
    std::cout << "Values: ";
    for(int n: values)
        std::cout << n << ' ';
    std::cout << '\n';
}

live demo online: http://ideone.com/TXm2GB
@kevinkjt2000, @Cubbi and @Raezzor, Thanks to all of you.

The idea was to simply have a function that returns a certain amount of values. In hindsight, thanks to you guys, the method that I used was pointless as I ended up not using the array that I placed in my parameter.
Last edited on
Topic archived. No new replies allowed.