I am using PRQA QA C++ as source code analyzer.
This is the first code I analyzed :
void test1(int * var);
void example1()
{
int var1[10];
test1(var1);
}
QA C++ told me
Array type is used as a pointer type argument in the function call.
So, I tried this following example (as it suggested) :
void test2(int (&var)[10]);
void example2()
{
int var2[10];
test2(var2);
}
This time, it told me :
Array type is used as a reference type argument in the function call.
Is there a better solution to use an array parameter ?
The original warning is fine, the second warning is also true.
This is due to arrays decaying to pointers, so var1
, originally an array of integers can be used in an expression requiring a pointer.
If you really want to remove these, there are several options:
std::array<int, 10> var1;
test1(var1.data());
Of better:
void test2(std::array<int, 10>& var);
void example2()
{
std::array<int, 10> var2;
test2(var2);
}
Then the second option fixes the size of the array. If it needs to be variable but fixed at compile time, use a template, otherwise use a std::vector
instead of a C-style array.