passing variable through std::array function prototype

How do I program the function prototype to have a variable element size so I can pass arrays of different sizes through the function?

1
2
void fx_sort_array(array<int,7>&my_array)
...

I've tried determining array length and passing it through the function prototype, but the compiler complains:

1
2
3
4
int size_array = my_array.size();
...
void fx_sort_array(array<int,size_array>&my_array)


I suspected this was wrong but gave it a try anyway.
This was easy through array ptrs.

complete 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
34
35
36
37
38
39
40
#include<iostream>
#include<array>
#include<algorithm>// for std::sort

using namespace std;

   void fx_sort_array(array<int,7>&my_array)
   {
	cout<<"sort_array forwards :"<<endl;
	sort(my_array.begin(), my_array.end());
   }

   void fx_sort_array_backwards(array<int,7>&my_array)
   {
	 cout<<"sort array backwards :"<<endl;
	 sort(my_array.rbegin(), my_array.rend());
   }

void print_array(array<int,7>&my_array)
{
	for(auto elem_num : my_array)
	{
	cout<<elem_num<<" ";
	}
	cout<<endl;
}

int main()
{
	array<int,7>my_array = {3,2,78,76,9,99,1};
	int size_array = my_array.size();
	cout<<"Array :"<<endl;
	print_array(my_array); //
	fx_sort_array(my_array);
	print_array(my_array);
	fx_sort_array_backwards(my_array);
	print_array(my_array);
	
	return 0;
}
Is there a reason you can't just use vectors?

I read a stackoverflow thread about this, they mentioned that you need to use templates, take a look - http://stackoverflow.com/questions/17156282/passing-a-stdarray-of-unknown-size-to-a-function
Last edited on
I guess I can...? Will research. For the time being, this is the exercise.
I can think of three ways to do it.

1. Let the array size be a template parameter (see link posted by TarikNeaj).

2. Write the function as you would have for raw arrays and take an int* as first argument and the size as a second argument. Calling the function would then look something like this: print_array(my_array.data(), my_array.size());

3. Pass an iterator pair to the function. This requires the use of templates. The page that TarikNeaj linked also contains an example of this (further down).
Last edited on
I haven't gotten to templates yet, so I picked #2.

Worked fine.

It appears the advantage of using a pointer based approach std::array->array.data() etc. allows the user to repeatedly pass the std::array container to a function without having the hard coding issue of the array element size when reusing the function for different arrays.

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
#include<iostream>
#include<array>
#include<algorithm>

using namespace std;

void print_array(int * n_ptr, int size)
{
	while(size-- > 0)
{
		cout<<*n_ptr<<" ";
		++n_ptr;
}
cout<<endl;
}

int main()
{
        std::array<int,7>my_array = {3,2,6,5,7,8,0}; //array 1 - my_array
        std::array<int,4>another_array = {3,9,2,13}; //array 2 - another_array

        cout<<"call_fx"<<endl;

	cout<<"array 1: ";
        print_array(my_array.data(), my_array.size());
	
	cout<<"array 2: ";
        print_array(another_array.data(), another_array.size());//fx call

	return 0;
}
Last edited on
It appears the advantage of using a pointer based approach std::array->array.data() etc. allows the user to repeatedly pass the std::array container to a function without having the hard coding issue of the array element size when reusing the function for different arrays.

The advantage has a cost. Whereas when a template function is used and the size is provided automatically, when the pointer approach is used there exists an opportunity for the size provided to the function not to be accurate.

IOW, you've increased the opportunity for bugs to appear in code using the function.
I agree that this coding strategy is not optimal. Look forward to using the template(s).

when the pointer approach is used there exists an opportunity for the size provided to the function not to be accurate


How is there an opportunity for size provided to be inaccurate?

you've increased the opportunity for bugs to appear in code using the function.


Why is this?

"hello" vector

Thx

Last edited on
How is there an opportunity for size provided to be inaccurate?

Is this a serious question? You provide the size rather than it being associated with the type. This means you can do things like: print_array(array, -1);
Yes, it is a serious question. I think its quite possible the question is lost in translation here between us. If you are referring to operator error I guess I get what you are saying. Otherwise no.

Further reflection I would guess that types would reduce this kind of error.
Last edited on
Topic archived. No new replies allowed.