Arithmetic on Pointers are MISRA compliant ?

6.17 Pointers and Arrays

Moderators: misra-c, david ward

Locked
lv
Posts: 9
Joined: Mon Oct 31, 2005 12:47 pm

Arithmetic on Pointers are MISRA compliant ?

Post by lv » Mon Oct 31, 2005 4:35 pm

The rules 17.1 to 17.3 give some possibilities to manipulate pointers (arithmetic, substraction, comparison) whereas the rule 17.4 doesn't authorize pointer manipulation in any way other than array indexing.

Should it be possible to explain me where I make a mistake in my understanding?

In particular, I would like to know if the following code is MISRA 2004 compliant concerning pointer manipulation:

Code: Select all

	unsigned int tab[10];
	void test(void)
	{
		unsigned int *ptr;

		ptr = &tab[2];
		*ptr = 33;
		ptr++;                         /*  MISRA Compliant ? */
		*ptr = 11;
	}
I thank you in advance.

Best regards.

Gavin McCall
Posts: 72
Joined: Mon Sep 13, 2004 9:24 am

Post by Gavin McCall » Thu Jan 05, 2006 4:48 pm

ANSWER: MISRA-C Steering Team 5/1/06

Your example is not compliant with 17.4.

Tab is an array type, therefore rule 17.1 is met. Rules 17.1, 17.2 and 17.3 address undefined behaviour issues.
It is recognised that 17.4 is a more restrictive rule than 17.1. Rule 17.1 still applies when rule 17.4 is deviated.

Use either

Code: Select all

tab[2] = 33;
tab[3] = 11;
tab[i+2] = …;
A deviation to rule 17.4 would need to be raised for direct pointer arithmetic on arrays as in your example above.[/b]
Gavin McCall
Personal view only.

Locked

Return to “6.17 Pointers and Arrays”