I’m new to iPhone development and I’m just trying out some simple drawing routines and I’m having trouble using defined values in simple math.
I have a line like this:
int offset = (((myValue - min_value) * 6) - middle);
and this works fine – but I don’t like using the hard coded 6 in there (because I’ll use it lots of places.
So I thought I’d define a constant using #define:
#define WIDTH_OFFSET 6;
then I could use:
int offset = (((myValue - min_value) * WIDTH_OFFSET) - middle);
however – this gets a compiler error : “Expected Expression.”
I can get round this by breaking up the calculation onto several lines:
int offset = myValue - min_value;
offset = offset * WIDTH_OFFSET;
offset = offset - middle;
The compiler thinks this is fine.
I’m guessing there’s some implicit cast or some other language feature at work here – can anyone explain to me what is happening?
Remove the semicolon
;after#define:#definesubstitutes its arguments literally, so your expression after preprocessing becomesAs you can see, there is a semicolon in the middle of the expression, which is a syntax error.
On the other hand, your other expression
does not exhibit such problem, because having two semicolons in a row as in
is syntactically valid.