interesting #define question

Guys,

I have an interesting question which is giving me a

"error C2010: '(' : unexpected in macro formal parameter list"
Which I would really like to get around. Basically my program makes a call to a function 'Digital_IO' upon which the value of the variable 'inputs' is returned. The value returned is an integer value between 0 & 64. The function represents the state of 6 binary inputs. ie 000101 = 5

What I am trying to achieve is putting a bitwise mask over this function so I can evaluate the states in individual manner.

This works:

Digital_IO(&inputs, outputs);
if(inputs & 0x01) IP1 = 1;
else IP1 = 0;

But has to be continually called within a while(1) to have any effect, and this really isn't very useful.

So I thought I could define this globally using:

#define IP1((inputs 0x01) ? 1 : 0); // if input 1=1, IP1=1, else IP1=0
But this generated a
"error C2010: '(' : unexpected in macro formal parameter list"

Which I pretty much expected because I have not played with functions in #defines.

Is there a way to have this bitwise mask continually applied when the program is executed, so I can only reference to the state of IP1, IP2, IP3 instead of inputs?

I am using visual c++ express 2008

Cheers,
Brett
Last edited on
What's interesting to you is boring to me. And I had my hopes up. You liar!
Macros are defined like this: #define ABS(x) (x<0)?-(x):(x)
Even if that wasn't the case, you didn't put the & between inputs and 1.

By the way, any expression using the ?: (e.g. x?1:0) operator, a 1 as the true operand, and a 0 as the false operand, is redundant and the ?: might as well be replaced by !! (e.g. !!x).
In your case, it may very well be left out, since all you're doing is checking the state of bit 0.
Last edited on
Helios, Can you provide an example relative to what I had posted?

I have tried this

#define IP1(inputs) (0x01) ?- (1) : (0);

and it seems to compile, but again, I'm missing the & for reference, but is this how its supposed to be?..

when I throw it in as

#define IP1(& inputs) (0x01) ?- (1) : (0);

It gives:
error C2010: '&' : unexpected in macro formal parameter list

I can't believe this is boring for you! I have not been able to figure this out!
The & is the AND bitwise operator, not the reference operator. PROTIP: if it's between two valid expressions, its the AND operator, otherwise it's the reference operator.
You don't need the macro, but just so you know how to do it next time:
#define IP1(x) ((x)&1)?1:0
Still not getting any output from defining this..

I have put in the code, which compiles

#define IP1(inputs) ((inputs)&1)?1:0

but this is still giving constant boolean zero as its output, regardless of the input state.

You are aware of the function "Digital_IO(&inputs, outputs);" the function does look like its passing by reference? I would attach the internals of it but it is linked to an external dll.

so say if I go

cout << inputs << "\n";

At present, it will show the state of inputs in either 1,2,3,4,........64

But I want to do something like

cout << IP1 << IP2.... etc and see all states.


It still isn't working!

Cheers, Brett
Last edited on
I'm at a loss, here.
Define in precise words what you need your function to do.

Oh, I just saw this:
Is there a way to have this bitwise mask continually applied when the program is executed, so I can only reference to the state of IP1, IP2, IP3 instead of inputs?

There is, but it's neither practical nor efficient.
Anything would help, I'm completely in a bind here.
You need to write a wrapper class for integer that will overload all arithmetical operators (+, -, *, /, %, &, |, ^, !, <, >, <=, >=, !=, ==, ||, &&, and I can't think any more, right now) and make it so that instead of using the actual value of the integer, they use integer&1 instead.
Then overload the assignment operator so that it will work directly on the data.
Then overload the other assignment operators (+=, -=, *=, and a long etc.) so that they take operate on the data using integer&1 as though it was the left hand operand.

I did say it wasn't practical, didn't I?
Ok, so not easy.

Is there another way to define these variables, or something that can be used within the program so I do not have to continually call the if, else loop of:

if(inputs & 0x01) IP1 = 1;
else IP1 = 0;

any time I wish to use IP1, IP2...? Surely you code gurus would have ran into this type of problem before!
You could use IP1=inputs&1;
I don't really see the need for this. if (x&1) is the very definition of perfection (i.e. something that can no longer be improved).
Ok, so I got it working,

#define IP1 ((inputs)&1)?1:0

works well.

an interesting thing is though, if I try to call IP1 in cout, in a while(1) I get

error C2297: '<<' : illegal, right operand has type 'const char [2]'

Which would lead me to believe there's something wrong in the #define, but if I go i=IP1 and then "COUT << i;" theres no probs.. I have to call Digital_IO(&inputs,outputs) before any assignment otherwise the assignment has no reference passed to it.

Cheers for your help so far Helios,

Brett
Last edited on
#define IP1 (((inputs)&1)?1:0)

For the love of god, get rid of the ?: operator! It's redundant as hell!
This will have the same effect.
#define IP1 ((inputs)&1)
Yes, do as helios' suggested. The problem you are seeing austfreddo is that you need to completely encapsulate the expression in parentheses as helios has done. Macros are textual substitutions, so what you are getting is this:

 
  cout << "Hello " << IP1 << " World!" << endl;


is being translated to

 
  cout << "Hello " << ((inputs)&1)?1:0 << " World!" << endl;


And due to operator precedence this is generating a compile error.

PS - I just love when I see this kind of code :
1
2
3
4
5
  bool flag;
  if( var == 0 )
     flag = true;
  else
     flag = false;


It's as if the boolean expression var==0 isn't good enough to be assigned to flag.

These are the kind of reasons why I try to implement all of my features with a negative NCSL... I usually end up deleting more useless code than I write... :)
Topic archived. No new replies allowed.