Help me understand this macro please!!!

S

s.subbarayan

Dear all,
I encountered a macro in one of the file i got from our software
vendor.This piece of code is used for tracing the debug messages.I am
not able to understand this line:

#define MASTER_STATE(p) ( ((p)->State) >=
STI2C_MASTER_ADDRESSING_WRITE )


So can some one explain me what it attempts to do?My main doubt is can
we have pointers like the ("p") present in the code above in macro
definitions?If so should we not intialise it before calling?And to my
amazement I never find any initialisation for p.also can we do type
checking inside a macro ?Like make sure the user passes only
integer?or is it a drawback of macros that they cant do any type
checking?

How can "p" point to state in above code with out initialising?

note:the "state" mentioned above is a structure variable.theres one
structure by name "statedef" and this "state" is a variable of type
"statedef".

I would appreciate any help and advanced thanks for the same.

Regards,
s.subbarayan
 
J

jota

#define MASTER_STATE(p) ( ((p)->State) >=
STI2C_MASTER_ADDRESSING_WRITE )

The preprocessor exchanges (p) to whatever you implement

Consider
------------------
#define STI2C_MASTER_ADDRESSING_WRITE 10
#define MASTER_STATE(p) ( ((p)->State) >= STI2C_MASTER_ADDRESSING_WRITE )
typedef struct S
{
int State;
};
int main()
{
S s1,s2;
s1.State=5;
s2.State=15;
if(MASTER_STATE(&s1))
{
..do stuff
}
if(MASTER_STATE(&s2))
{
..do stuff
}
retrun 0;
}
------------------
The preprocessor replaces the implementation with
MASTER_STATE(&s1) to
....( ((&s1)->State) >= 10)

MASTER_STATE(&s2) to
....( ((&s2)->State) >= 10)
//jota
 
S

Saroj Pattnaik

The peice of code will be handled by your preprocessor and the line is a
decalration. Preprocessing is done before C syntax checking. You can use
this as if a function. Now the condition can be replaced in any
statements. It eases out the Code.

thanks
saroj
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top