5
#define MIN(A,B) ((A) <=  (B) ? (A) : (B))

this is the macro , I was asked what's the side effect if I used the following :

least = MIN(*p++, b);

Note: This was embedded c question

Matteo
  • 14,696
  • 9
  • 68
  • 106
xsari3x
  • 442
  • 2
  • 12
  • 36

4 Answers4

8

It evaluates p++ twice. Also, since the first evaluation changes p, the second time around it will point to a different element. So the returned value will be *(initialp + 1) or b.

You should try it yourself.

cnicutar
  • 178,505
  • 25
  • 365
  • 392
7

The macro will expand to:

least = ((*p++)<=(b)?(*p++):(b))

you will have then *p++ twice in your statement (i.e., it will be incremented twice).

Matteo
  • 14,696
  • 9
  • 68
  • 106
  • Just a comment not completely related to the question: this is one of the reasons why many languages don't have macros: the can obfuscate your code. – Matteo Sep 04 '11 at 13:13
2

*p++ gets evaluated twice as the macro expands to *p++ <= b ? *p++ : b

Also, there is no such thing as "embedded C".

Gregory Pakosz
  • 69,011
  • 20
  • 139
  • 164
1

Assume initial address of p = 0xfcf0, *p = 1, b = 2, value @ 0xfcf4 = 5 and value @ 0xfcf8 = 15

The macro will expand as

least = ((*p++) <= (b) ? (*p++) : (b));

i.e least = ((1) <= (2) ? (*p++) : (b));

since *p is incremented twice.

1) *p++ --> now p will point to address 0xfcf4;

2) *p++ --> now p will point to address 0xfcf8;

So least = 15; (the values in the address 0xfcf8). Hope it helps.

Ashok
  • 11
  • 1