Hello people,
How do u add 2 numbers without using the plus operator.
This is 1 of the many methods i came across.
I allllmost got it, but not quite.
This is the code i followed
-------------------------
Code:#include< stdio.h > int main() { int a=30000,b=20,sum; char *p; p=(char *)a; sum= (int)&p[b]; //adding a & b printf("%d",sum); return 0; }
This is my understanding until now
---------------------------------
1)The number 30000 is converted to a char pointer. So if u inspect p, it comes to 0x7530.
2)And b==20==0x14.
3)Now p[b] == *(p+b) , and &p[b] == &*(p+b) == p+b
4)Now p+b holds 0x7530 + 0x0014 = 0x7544
5)Then typecast 0x7544 into an int == 30020 == CORRECT SUM
My only doubt is , how can we convert an integer to a pointer like that. ( i mean , typecasting 30000 to 0x7530)
I mean, thats weird.
Is the answer that i just have to believe it.
Can some1 lend more light on this topic and probably even tell me a quick line about typecasting ?