Rule 17.4 - array elements contained inside structure def

6.17 Pointers and Arrays

Moderators: misra-c, david ward

Post Reply
sshidore
Posts: 1
Joined: Fri Jan 18, 2013 2:13 pm
Company: Emerson

Rule 17.4 - array elements contained inside structure def

Post by sshidore » Mon Jan 21, 2013 8:08 am

• MISRA C rule 17.4 says arithmetic operations shall not be performed on pointers, as they may result into referencing to non-existing addresses.
• Alternative to this is to catch a pointer being received as an argument into the respective type of an array, for e.g. catch ‘&arr’ into ‘arr[]’, and use array indexing to access its elements. This works well for pointers of basic data types.
• Problem arises in the case of accessing array elements contained by a structure.
• e.g.
struct message
{
int address;
int length;
int data[50];
}m1,m2;

• Now, the statement “source_node = m1.data[SOURCE_NODE_INDEX]” triggers this 17.4 non-compliance note.
• This happens when accessing only the array elements, and not for single variables of a structure definition.
• So the question I have is, what is the best way to access individual array elements contained inside a structure definition?

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

Re: Rule 17.4 - array elements contained inside structure de

Post by misra-c » Tue Jan 29, 2013 8:36 am

This code does not violate Rule 17.4. Array elements within a structure can be accessed in this manner.
---
Posted by and on behalf of
the MISRA C Working Group

Post Reply

Return to “6.17 Pointers and Arrays”