Let's start by getting rid of the templates, as they make things more confusing. In the following, I replaced T
with int
.
void Fill_With(int target_array, int value){
int Array_Size = sizeof(target_array) / sizeof(int);
for(int l = 0; l < Array_Size; ++l){
target_array[l] = value;
}
}
Let's try it out! I pasted this into the Arduino IDE and compiled it. Here is the output:
sketch_aug01a/sketch_aug01a.ino: In function 'void Fill_With(int, int)':
sketch_aug01a:4: error: invalid types 'int[int]' for array subscript
target_array[l] = value;
^
exit status 1
invalid types 'int[int]' for array subscript
This is telling us that we're trying to subscript (that's the term for the []
syntax) a variable of type int
with a variable of type int
. In C++, a pointer to the start of an array has type int *
, not int
. Let's fix the type of target_array
in the function signature:
void Fill_With(int *target_array, int value){
It compiles, but if you ran it you would quickly run into a bug:
- When given an expression, the
sizeof
operator will return the number of bytes in the type of the expression. The type of the expression target_array
is int *
, so sizeof(target_array) == sizeof(int *)
. On 32-bit systems, sizeof(int *) == 32/8 == 4
.
- When given a type, the
sizeof
operator returns the number of bytes in the type. So sizeof(int)
gives the number of bytes in an int
, which is guaranteed to be at least 2 bytes (16 bits), but is usually 4 or 8 (it depends on the system).
Together, this means that Array_Size
ends up being some small number like 0 or 1, regardless of the contents of target_array
. To fix this, you can instead pass in the length of the array as a parameter:
void Fill_With(int *target_array, int array_size, int value){
for(int l = 0; l < array_size; ++l){
target_array[l] = value;
}
}
int array1[4] = {1,2,3,4}, k = 0;
Fill_With(array1, 4, k);
Now you can put the templates back in:
template <class T> void Fill_With(T *target_array, int array_size, T value){
for(int l = 0; l < array_size; ++l){
target_array[l] = value;
}
}
int array1[4] = {1,2,3,4}, k = 0;
Fill_With<int>(array1, 4, k);
By the way, C++ already has this function built in. It's called std::fill
. Here's how to use it:
#include <algorithm>
int array1[4] = {1,2,3,4}, k = 0;
std::fill(array1, array1 + 4, k);