Rule 17.5 and arrays of size 1

Moderators: misra-c, david ward

Post Reply
gdavis
Posts: 3
Joined: Tue Oct 05, 2010 11:48 pm
Company: Green Hills Software

Rule 17.5 and arrays of size 1

Post by gdavis » Tue May 15, 2018 11:09 pm

Hello,

Please consider the following case:

Code: Select all

#include <stdint.h>
extern void f(int32_t x[1]);
int32_t glob;
extern void test(void);
void test(void)
{
    f(&glob); /* Allowed under MISRA C 2012 R17.5? */
}
Could you confirm if the intent that this should be a violation of the rule since we are passing a non-array as an argument where an array is expected? I ask only because there is related wording in the C90/99 standard under additive operators that states:
For the purposes of these operators, a pointer to a nonarray object behaves the same as a
pointer to the first element of an array of length one with the type of the object as its element
type.
Thank you for your help.

Best Regards,

-Greg

misra-c
Posts: 556
Joined: Thu Jan 05, 2006 1:11 pm

Re: Rule 17.5 and arrays of size 1

Post by misra-c » Fri Jun 08, 2018 8:35 am

Your example is not a violation of rule 17.5. The Amplification states that the argument
... should point into an object that has at least as many elements as the array.
As you quoted, the C standard states that "&glob" should behave as if it were a pointer to the first element of an array of length one. The expected size in "f" is one, and so the example is compliant.
---
Posted by and on behalf of
the MISRA C Working Group

Post Reply

Return to “8.17 Functions”