Clear Filters
Clear Filters

How to eliminate MISRA 11.3 warning with 2D Array Argument?

24 views (last 30 days)
Consider this sample code. I want to pass a 2-d array in to a function as a non modifyable argument.
If I use this notation, I get a MISRA warning 11.3 " A cast shall not be performed between a pointer to object type and a pointer to a different object type. " . If I leave out the const keyword, I get a MISRA warning that pointer arguments should be constant if not modified.
Is there a simple way to do this and eliminate the 11.3 warning?
typedef signed short int S16; /* ss_ */
typedef unsigned char U8; /* uc_ */
static void TEST_FUNC(const S16 loc_testVar[4][4] );
void MAIN(void)
{
/* const */ S16 testVar[4][4] = {{1,2,3,4},{1,2,3,4},{1,2,3,4},{1,2,3,4}};
TEST_FUNC(testVar); //MISRA warning 11.3 here
}
static void TEST_FUNC(const S16 loc_testVar[4][4])
{
U8 i, j;
S16 newVar[4][4];
for (i=0; i<4; i++)
{
for (j=0; j<4; j++)
{
newVar[i][j] = loc_testVar[i][j];
}
}
loc_testVar[1][1] = 3; // this should cause an error if loc_testVar is actualy constant!
}

Answers (1)

Yash
Yash on 4 Oct 2023
The best way to address the issue and eliminate the MISRA warning 11.3 is to make the array constant. By declaring the array as constant, you clearly indicate that you don't intend to modify it within the function, which aligns with MISRA guidelines and ensures safety and maintainability of your code.
So, in your code, you can declare the "testVar" array as constant like this:
const S16 testVar[4][4] = {{1,2,3,4},{1,2,3,4},{1,2,3,4},{1,2,3,4}};
This change will eliminate the warning.
You can read more about the MISRA C rule 11.3 here : https://in.mathworks.com/help/bugfinder/ref/misrac2012rule11.3.html
I hope this helps.
  1 Comment
Moses Fridman
Moses Fridman on 6 Oct 2023
Thanks for your comment, but in my case the 2d array needs to be modifiable, because it’s contents are calculated at runtime. Hence the issue.

Sign in to comment.

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!