To pass by reference the C-style array as a parameter to a function, one needs to send the array size as a parameter to the function as well. The below code shows a function that accepts an array as pass by reference along with its size. Q1) Is there a way, we can pass arrays by reference without passing its size as well? Q2) If I were to overload the below function get_num_of_even_digited_input() to accept an array container (std::array) as the first parameter, what would the function prototype look like? (I mean how do I mention the size_t parameter for the array? Will it be int get_num_of_even_digited_input(array<int,??> )
int get_number_of_digits(int in_num)
{
int input = in_num, rem = -1, dividend = -1, dig_count = 0;
while(input)
{
dig_count++;
dividend = input % 10;
input = input/10;
}
return dig_count;
}
int get_num_of_even_digited_input(int arr[], int size_arr)
{
int arrsize = 0, i = 0, count_of_evendigited_num = 0, count_of_dig = 0;
arrsize = size_arr;
for(i = 0; i < arrsize; i++, arr++)
{
count_of_dig = get_number_of_digits(*arr);
if(count_of_dig % 2 == 0)
{
count_of_evendigited_num++;
}
}
return count_of_evendigited_num;
}
void main()
{
int array2[] = {23, 334, 4567, 1, 259};
int sizearr2 = sizeof(array2)/sizeof(int);
array<int, 6> array3 = {23, 5677, 564, 342, 44, 56778};
cout << "Count of even digited numbers in array2[] : " << get_num_of_even_digited_input(array2, sizearr2) << endl;
cout << "Count of even digited numbers in array3[] : " << get_num_of_even_digited_input(array3, array3.size) << endl; // HOW IS THIS FUNCTION PROTOTYPE GOING TO LOOK LIKE??
}