5

I'm getting error like "expected an statement"

my code is as follows

#define IN_Tamper    0X00001000     /*P2.12 = EINT2*/
#define DIR_IN_Tamper    { FIO2DIR &= ~0X00001000 ; } 

/* main */
DIR_IN_Tamper(); 
if(((IN_Tamper >> 12) & 0x01) == 1)
            BUZZER_ON();
         else
            BUZZER_OFF();   

I'm getting error saying

  1. Expected an statement for DIR_IN_Tamper();

  2. expected a statement for the else part.....

6
  • or defined like: #define DIR_IN_Tamper(FIO2DIR) { FIO2DIR &= ~0X00001000 ; }; and call like DIR_IN_Tamper(FIO2DIR); what is FIO2DIR ? Commented Jul 12, 2013 at 10:29
  • #define DIR_IN_Tamper { FIO2DIR &= ~0X00001000 ; } like this...... Commented Jul 12, 2013 at 10:31
  • No I mean what is FIO2DIR ? if its macro then consider @phihag's answer. If its variable you wants to pass then defined like macro function as I suggested. Commented Jul 12, 2013 at 10:33
  • FIO2DIR is ARM7's key word... Commented Jul 12, 2013 at 10:38
  • 1
    lastly I have removed () from every where n its working fine.. thank you all.. Commented Jul 12, 2013 at 11:39

5 Answers 5

7

The C preprocessor is (at least in the way you use it) just a simple search-and-replace, so you're effectively running

/* main */
{ FIO2DIR &= ~0X00001000 ; } (); 

This doesn't make any sense. Remove the parentheses in the line

DIR_IN_Tamper(); 

For BUZZER_ON and BUZZER_OFF, you want to remove the parentheses as well. If the macro isn't enclosed in curly braces, you also want to add those, like

if(((IN_Tamper >> 12) & 0x01) == 1) {
    BUZZER_ON
} else {
    BUZZER_OFF
}
Sign up to request clarification or add additional context in comments.

2 Comments

for BUZZER_ON/OFF I have used macro
like #define DIR_BUZ() FIO0DIR |= 0x10000000 ; //DIRECTION FOR BUZ PIn P0.28 #define B_BUZ_E(X) {(X==SET)? (FIO0SET |= PINO_BUZ) : (FIO0CLR |= PINO_BUZ) ;} #define BUZZER_ON() { DIR_BUZ(); B_BUZ_E(1); } #define BUZZER_OFF() { DIR_BUZ(); B_BUZ_E(0); }
2

DIR_IN_Tamper is defined as { FIO2DIR &= ~0X00001000 ; }, therefore when the preprocessor parses your code, this line

DIR_IN_Tamper(); 

is converted into

{ FIO2DIR &= ~0X00001000 ; }()

Which is clearly not correct. Not sure what exactly you're trying to achieve, but removing the parentheses will eliminate the syntax error:

DIR_IN_Tamper

Further to it, I suspect you have similar issues with BUZZER_ON and BUZZER_OFF.

5 Comments

Shouldn't DIR_IN_Tamper rather be defined as a function-like macro?
Maybe it should but I see the question unclear in terms of what he exactly wants to do
@undur_gongor Maybe it should be, but, as KiaMorot pointed out, it's rather unclear from the question what the OP is trying to do. I showed one way to syntax errors.
if I remove {} it gives error error: #109: expression must have (pointer-to-) function type
@user2571585: You have to remove the normal parentheses (()), not the curly braces.
2

If you want to use DIR_IN_Tamper like a function, you need a function-like macro:

#define DIR_IN_Tamper()    { FIO2DIR &= ~0X00001000 ; } 

Then, a better way to do it is:

#define DIR_IN_Tamper()    do { FIO2DIR &= ~0X00001000 ; } while(0)

... but that's a different story.

7 Comments

Yes, it is a macro. But you try to "invoke" it like a function (with ()). Therefore you need a function-like macro.
macro of BUZZER_OFF is as follows-
#define BUZZER_OFF() { DIR_BUZ(); B_BUZ_E(0); }
do I need to remove () or {} here also..?? its not single statement but.
First and foremost, you should add the definitions of the other macros to your question.
|
1

Single-statement, function-like macros

Please do not use curly braces ({ and }) when defining single-statement macros like DIR_IN_Tamper.

To safely define a function-like macro, simply put your definition between parentheses, like this:

#define DIR_IN_Tamper() (FIO2DIR &= ~0X00001000)

Then, call your macro like this:

DIR_IN_Tamper();

It will behave like a functions which changes the value of FIO2DIR and then returns the changed value:

/* Your macro rewritten as a function.
   The return type should be the type of FIO2DIR */
uint32_t DIR_IN_Tamper()
{
    return (FIO2DIR &= ~0X00001000);
}

Multi-statement, function-like macros

If you ever need to define a multi-statement macro, see this other C FAQ entry.

For example, define BUZZER_OFF as:

#define BUZZER_OFF() do { DIR_BUZ(); B_BUZ_E(0); } while (0)

5 Comments

Armaccess.c(973): error: #109: expression must have (pointer-to-) function type
@user2571585: You have to remove the normal parentheses (()), not the curly braces.
@user2571585 I commented something about {} to your question
I have removed () braces from main.. now it looks like BUZZER_OFF; but still error in else
When in doubt, look at the pre-processed source: see stackoverflow.com/q/3742822/238421.
0

Macros in C are not functions. DIR_IN_Tamper(); should be DIR_IN_Tamper;.

6 Comments

It is like this #define DIR_IN_Tamper FIO2DIR &= ~0X00001000 & not like u have mentioned..
Huh? I mean the call. Not the macro definition. Seems you're too much confused about C. You should get a decent C book and read it before writing code.
Then make a question about your new error, or say what error it is.
code says as follows DIR_IN_Tamper; if(((IN_Tamper >> 12) & 0x01) == 1) BUZZER_ON; else BUZZER_OFF; where in BUZZER_OFF is again a macro defined as #define BUZZER_OFF { DIR_BUZ; B_BUZ_E(0); } since its group of statements I have used { } braces...
Mu advice is that you avoid macros in C, they're more complicated and prone to errors than you might think. This explains why.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.