int foo(const uint8_t array[]) {
int x;
for(i=0;i<5;i++){
x= array[i];
}
return 0;
}
it gives a warning as below,
"parameter array could be declared const" ==> i already have declared the array const, i am programming in C++.
The MISRA C and MISRA C++ standards are a set of coding guidelines for the C and C++ programing languages that promote safety, security, and reliability in embedded system software.
MISRA-C:1998 has 127 rules, of which 93 are required and 34 are advisory; the rules are numbered in sequence from 1 to 127.
MISRA® is a set of C and C++ coding standards, developed by the Motor Industry Software Reliability Association (MISRA). What's more, MISRA a top coding standard for embedded industries, including automotive. And, MISRA ensures that C/C++ code is safe, secure, and reliable.
First thing to note is that int foo(const uint8_t array[])
is equivalent to int foo(const uint8_t* array)
, i.e. the function takes a pointer to a const uint8_t
, not an array. The pointer itself it not const
, the pointee is. The signature should be:
int foo(const uint8_t* const array)
For the record, I don't find this warning particularly useful. The parameter is taken by value and the caller couldn't care less what the function does with it. Furthermore, top level const qualifiers on parameters are ignored when comparing function signatures, and this can lead to some confusion.
void foo(int)
and void foo(const int)
, for example, are identical signatures.
EDIT:
So, according to your comment, MISRA doesn't know that you can't pass arrays by value and complains that array indexing works differently than pointer arithmetic. Shudder... The problem is that you can't add top level const
using the array syntax, which makes fixes to these two warnings mutualy exclusive.
Try tricking it like this, then:
typedef const uint8_t Array[];
int foo(const Array arr);
Remember that, despite the syntax, the function actually takes a pointer, and is equivalent to
int foo(const uint8_t * array)
So array
points to an array of constant bytes; but is not itself constant. The warning is pointing out that, since the function doesn't modify the pointer, it could (and, at least according to this rule, should) be constant:
int foo(const uint8_t * const array)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With