passing std::array to a function

I need help with coding an example of an std::array being passed into a function. I then will then access the function to demonstrate the array doesn't decay to a ptr when passed to a it.

1
2
3
4
5
6
7
8
9
10
11
12
13

void fx_print_element_num(int my_array)
{
cout<<my_array.size()<<endl;
}

int main()
{
std::array<int, 6> my_array = {12,2,99,3,45,6};
funct(my_array);

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <array>
#include <iostream>

void funct(std::array<int, 6> my_array)
{
  std::cout<<my_array.size()<<std::endl;
}

int main()
{
std::array<int, 6> my_array = {12,2,99,3,45,6};
funct(my_array);

}
Moschops why is the prototype of funct the way that it is. Meaning, is this something just to learn, or is there a logical reason for its lengthy coding?

Thz in advance.
What prototype? That's the function itself, it doesn't need a function prototype since it's defined above main.
Meaning, is this something just to learn, or is there a logical reason for its lengthy coding?

The type of the parameter should match the type of the object you want to feed to it...


... or this is one of those areas where templates may actually make your code easier to read.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <array>
#include <iostream>

template <typename T>
void print_size(const T& my_array)
{
    std::cout << my_array.size() << '\n';
}

int main()
{
    std::array<int, 6> my_array = { 12,2,99,3,45,6 };
    print_size(my_array);
}


with the added benefit that the template function will work for any object with a size member function that returns a type that std::cout knows what to do with.
Hi Tarik you were absolutely correct. There was no function prototype in that example. You forced me to review and here is the corrected version below.

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
#include<iostream>
#include<array>
#include<algorithm>// for std::sort

using namespace std;

//prototypes

void fx_sort_array_forwards(array<int,7>&my_array);
void print_array(array<int,7>&my_array);

int main()
{
	array<int,7>my_array = {3,2,78,76,9,99,1};
	cout<<"array: "<<endl;
	print_array(my_array);
	fx_sort_array_forwards(my_array);
	print_array(my_array);
	return 0;
}

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

void print_array(array<int,7>&my_array)
{
	for(auto elem_num : my_array)
	{
	cout<<elem_num<<" ";
	}
	cout<<endl;
}
The type of the parameter should match the type of the object you want to feed to it...


The function parameter print_array(my_array);
has the same type as array<int,7>&my_array?

int -?

I understand how to make them work together but why do they work together?

Haven't gotten to templates yet.
Last edited on
The function parameter print_array(my_array);
has the same type as array<int,7>&my_array?


When you feed my_array to the function, my_array is an argument. The parameter is the named variable defined in the function header which takes on the value of the argument when the function is invoked.


I understand how to make them work together but why do they work together?

In C++, the type of the parameter determines what types the function will accept as arguments. If an argument is not the same type, or of a type implicitly convertible to the parameter's type, then you cannot call the function with that argument.
thx, got it
Topic archived. No new replies allowed.