4.1 Array-to-pointer conversion

4.1.1 Ensure that a function argument does not undergo an array-to-pointer conversion

When an array is bound to a function parameter of pointer type the array is implicitly converted to a pointer to the first element of the array. In order to retain the array dimension, the parameter should be changed to a reference type or changed to a user defined type such as std::array.

#include <cstdint>
#include <array>
               
void f (int32_t a[10]); // parameter is of pointer type
void g (int32_t a[]);   // parameter is of pointer type
void h (int32_t * a);
void i (int32_t (&a) [10]);
void j (std::array<int32_t, 10> & a);
               
void foo ()
{
int32_t a1 [10];
std::array<int32_t, 10> a2;
                 
f(a1); // @@- Non-Compliant -@@
g(a1); // @@- Non-Compliant -@@
h(a1); // @@- Non-Compliant -@@
i(a1); // @@+ Compliant +@@
j(a2); // @@+ Compliant +@@
}

References

  • JSF AV C++ Rev C – 97
  • MISRA C++:2008 – 5-2-12

Click here for references