What's the side effect of the following macro in C ? Embedded C

前端 未结 4 1086
野趣味
野趣味 2020-12-07 02:39
#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 :

lea         


        
4条回答
  •  生来不讨喜
    2020-12-07 03:06

    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.

提交回复
热议问题