i have just one question:

can a function return array?

Recommended Answers

All 8 Replies

It can return all sorts of things. So does Google.

do you know maybe some solution to that?

pass an array to it and let it edit that arrays data

can a function return array?

if a function has array as parameter ,
the array be pass by reference,
so if you modifying this array in this function the origin array is modifying without need the return ;

As stated, not directly.

But if you really, really think you need to do so, you can wrap an array inside a struct, which can be returned. It's not necessarily a wise thing to do.

Example

#include <iostream>
using namespace std;

struct foo
{
	int arr[5];
};

foo bad( )
{
	foo a;
	int k;

	for( k = 0; k < 5; k++ )
		a.arr[k] = k;

	return a;
}

int main()
{
    int i;

	foo my_foo;

	my_foo = bad( );

	for( i = 0; i < 5; i++ )
		cout << my_foo.arr[i] << endl;

	return 0;
}

(donning flak jacket now!)

Another way:

#include <iostream>

template<typename t, size_t s>
struct Array {
  t buffer[s];

  operator t*() {
    return buffer;
  }
};

Array<int, 5> getNums() {
  Array<int, 5> nums;
  nums[0] = 0;
  nums[1] = 1;
  nums[2] = 2;
  nums[3] = 3;
  nums[4] = 4;
  return nums;
}

int main() {
  Array<int, 5> nums = getNums();
}
commented: elegantly minimal +3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.