sum (adding 2 numbers ) without plus operator

后端 未结 9 674
借酒劲吻你
借酒劲吻你 2021-02-05 20:42

Can anyone explain the logic how to add a and b?

#include 

int main()
{
     int a=30000, b=20, sum;
     char *p;
             


        
9条回答
  •  广开言路
    2021-02-05 21:37

    p[b] is the b-th element of the array p. It's like writing *(p + b).

    Now, when adding & it'll be like writing: p + b * sizeof(char) which is p + b.
    Now, you'll have (int)((char *) a + b) which is.. a + b.

    But.. when you still have + in your keyboard, use it.


    As @gerijeshchauhan clarified in the comments, * and & are inverse operations, they cancel each other. So &*(p + b) is p + b.

提交回复
热议问题