Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective c : Using #define constant values in math expressions

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?

like image 974
Pete McPhearson Avatar asked Jan 23 '12 11:01

Pete McPhearson


4 Answers

Remove the semicolon ; after #define:

#define WIDTH_OFFSET 6

#define substitutes its arguments literally, so your expression after preprocessing becomes

(((myValue - min_value) * 6;) - middle);

As 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

int offset = myValue - min_value;
offset = offset * WIDTH_OFFSET;

does not exhibit such problem, because having two semicolons in a row as in

 offset = offset * 6;;

is syntactically valid.

like image 76
Sergey Kalinichenko Avatar answered Oct 08 '22 01:10

Sergey Kalinichenko


When you #define something where you use it is exactly the same as if you typed it in yourself there. So where you are using WIDTH_OFFSET you are getting 6; in its place - which of course is not your intention. So just remove the semicolon.

like image 21
Jeshua Lacock Avatar answered Oct 08 '22 01:10

Jeshua Lacock


As dasblinkenlight said, remove the semi colon. The explanation for this is that #defines are a literal substitution into your code. Thus with the semi colon your broken code read:

int offset = (((myValue - min_value) * 6;) - middle);

The working code read:

offset = offset * 6;;

Which is syntactically fine as the ;; is effectively a blank 'line' of code.

like image 41
Diziet Avatar answered Oct 08 '22 00:10

Diziet


Basically, macros are convenient functions that are placed inline by the preprocessor. So you can think that they are doing copy/paste for matching entries, in your case it will substitute any occurency of WIDTH_OFFSET with 6; so, just like others said, remover the semicolon ; and you are all set.

Also, when defining macros for simple math functions, remeber to put them in brackets ( and ) otherwise, you could end up with some math operation order bugs( like unintended part multiplication before addition)

like image 31
Michał Zygar Avatar answered Oct 07 '22 23:10

Michał Zygar