Page 1 of 1

About 17.4 Question?

Posted: Thu May 05, 2016 10:36 am
by hutry999
I need read some parameters from EEPROM. My Code as below:

typedef struct
Uint16 ErrorCode;
Uint16 ErrDCVoltage;
Uint16 ErrMCUState;
Uint8 ErrMotorModeCmd;
Uint8 ErrDriveState;
Uint16 ErrTorqueCmd;
Uint16 ErrTorqueFed;
Uint16 ErrSpeedSet;
Uint16 ErrSpeed;
Uint8 ErrIGBTTemp;
Uint8 ErrMotorTemp;
Uint16 ErrUd;
Uint16 ErrUq;
Uint16 ErrIdRef;
Uint16 ErrIqRef;
Uint16 ErrIdFed;
Uint16 ErrIqFed;
Uint16 ErrState;
Uint8 Err12V;
Uint8 Angle;
Uint8 AngleInit;
Uint8 ErrMCUTemp;
Uint16 DC_Cur;

void LoadError(ErrorLog_t* Err,Uint16 Index)
Uint16 i;
Uint16 *PrErrLog;
PrErrLog = (uint16 *)Err;
PrErrLog = PrErrLog + i;
GetEEPValue = ReadEeprom(Adr + i*2u);
*PrErrLog = GetEEPValue;

this line code "PrErrLog = PrErrLog + i;" not allowed 17.4

#1476-D (MISRA-C:2004 17.4/R) Array indexing shall be the only allowed form of pointer arithmetic EepromManage.c /SynBase line 292 C/C++ Problem

How do I deal with this situation?
only deal like this:
Err->ErrorCode = ReadEeprom(0u);
Err->ErrDCVoltage = ReadEeprom(1u);
Err->ErrMCUState = ReadEeprom(2u);

if my struct has more than 100 items, What can I do?

Re: About 17.4 Question?

Posted: Mon May 09, 2016 6:44 am
by dg1980
Can't you do something like this:

Code: Select all

void LoadError(ErrorLog_t Err[],Uint16 Index)
  Err[Index] = /*...*/;

Re: About 17.4 Question?

Posted: Fri Jun 24, 2016 1:40 pm
by misra-c
There are a number of issues with this example as written.
  • The line "PrErrLog = (uint16 *)Err;" violates rule 11.4.
  • Cast is to uint16 not Uint16
  • Pointer increment is not consistent, but varies from 0 to SizeErrLog. So the accessed items are 0, 1, 3, 6 ...
  • Using a non-character pointer to access a struct whose members are of different sizes is not a good idea. Accessing using a non-character pointer is implementation dependent
In some circumstances it might be appropriate to access a structure using a pointer. In those cases a deviation is required for this rule. An example of the issues that need to be considered when deviating this rule ( and rule 17.1) can be found in
"MISRA C:2004 Permits Deviation permits for MISRA Compliance"
which are available from the "MISRA resources" section of the MISRA forum (

The MISRA working group can not give advice on particular coding design issues.