error: expression must have integral type
8 views (last 30 days)
Show older comments
Hello,
I received the following error "expression must have integral type".
It is related to the C-code that I have in the block "C-script" from Plecs blockset.
This is the code:
#include <stdio.h>
#include <stdlib.h>
#include <float.h>
#include <math.h>
#define PHI Input(0)
#define PHIOUT Output
#define DEBUG_OUT1 Output(6)
#define DEBUG_OUT2 Output(7)
#define STATE DiscState(0)
#define PHI_1 DiscState(1)
#define PHI_2 DiscState(2)
#define TRANS DiscState(3)
#define MODE ParamRealData(0, 0)
#define ftau ParamRealData(1 ,0)
float a1;
float a2;
float k;
#define gamma(phi) exp(phi/ftau)
STATE = 0;
PHI_1 = 0;
PHI_2 = 0;
TRANS = 0;
k = exp(-1/(6*ftau));
a1 = k*k*k/(k*k*k + 1.f);
a2 = 1.f/(k*k*k + 1.f);
//float tran_phi;
switch((int)MODE) {
case 1:
if (TRANS == 1)
// PHIOUT(STATE) = 0.93*PHI_1;
PHIOUT(STATE) = PHI_1;
else if (TRANS == 2)
// PHIOUT(STATE) = 1.07*PHI_2;
PHIOUT(STATE) = PHI_2;
else
PHIOUT(STATE) = PHI_2;
break;
case 2:
if (TRANS) {
// PHIOUT(STATE) = (0.93*PHI_1 + 1.07*PHI_2)/2;
PHIOUT(STATE) = (PHI_1 + PHI_2)/2;
} else {
PHIOUT(STATE) = PHI_2;
}
break;
case 3:
if (TRANS) {
// IICC 3-step method switching timing
PHIOUT(STATE) = ftau*log(a1*gamma(PHI_1) + a2*gamma(PHI_2)); // intermediate load angle
} else {
PHIOUT(STATE) = PHI_2;
}
break;
default:
PHIOUT(0) = PHI;
PHIOUT(1) = PHI;
PHIOUT(2) = PHI;
PHIOUT(3) = PHI;
PHIOUT(4) = PHI;
PHIOUT(5) = PHI;
break;
}
DEBUG_OUT1 = TRANS;
DEBUG_OUT2 = STATE;
STATE = ((int)STATE + 1)%6; // States 0-6
if (TRANS) {
TRANS--;
} else if (PHI != PHI_2) {
PHI_1 = PHI_2;
PHI_2 = PHI;
switch((int)MODE) {
case 1:
TRANS = 2;
break;
case 2:
TRANS = 3;
break;
case 3:
TRANS =3;
break;
default:
break;
}
}
There are several identical errors ("error: expression must have integral type"), which point at the following lines of code:
PHIOUT(STATE) = PHI_1;
PHIOUT(STATE) = PHI_2;
PHIOUT(STATE) = (PHI_1 + PHI_2)/2;
PHIOUT(STATE) = PHI_2;
Thank you for your help.
2 Comments
Jan
on 11 May 2018
Inserting a blank line after each line of code reduces the readability substantially. Please post the complete error message. What does "is related to the lines" mean? An error is cause by one command only. Please specify, which command this is.
Answers (1)
Wolfgang Hammer
on 11 May 2018
You have defined "STATE" as a (discrete) state variable, which is of type "double". Your macro "PHIOUT", on the other hand, resolves into an array access (with the argument used as an index).
Now, the C-Script will apparently make the cast from "double" to "int" and so your code will run inside PLECS. However, if you generate code from your model and try to compile this with another compiler, this other compiler may complain about this use of a non-integral expression as an array index.
This is not a problem of PLECS or the PLECS Coder but of your own C code. You might be able to fix this by explicitly casting the variable STATE to an integer, e.g.:
PHIOUT((int) STATE) = PHI_1;
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!