Ok, this is idiotic.
Anyone found giving that example as h/w should be questioned for sanity.
First the explanation part, and to understand that, you need to have a brief idea of what pointers are:
char *p = "Hello" is stored as: |H|e|l|l|o|
here p points to the first memory location which can be any long type integer. Suppose it's 1337. Then p+1 = 'e', p+2 = 'l', etc. It's just relative addressing from there on.
If this were an int the increments would be 2 words, albeit (p+1) would give the next int location, nonetheless.
So here's what this retard did:
1. Input 2 integers: say 1337 & 3
2. Point to location 1337 (p of type char*)
3. Get address of p
-> Here's the catch: array[index] can also be written as *(array+index) since the array name points to the first element of the array.
So, tada! you get &(*array+index), which after cancelling reference & deference comes out to be: (array + index) = 1337 + 3
4. This is cheating (apart from being retarded), he used arithmetic operator, albeit implicitly.
@gameranand, the code is perfectly valid, unless you're reading the value of the memory. It should not crash on gcc.
If I were to do it, I'd do it the PROPER way:
Code:
int main(int argv, char **argc)
{
int a=1337,b=10,sum;
short _a = 0, _b = 0, c = 0;
short mask=1;
short s=0;
unsigned int bits = 2147483648;
while(bits >>= 1)
{
_a = a & mask;
_b = b & mask;
s |= ((_a ^ _b) ^ c) & mask ;
mask <<= 1;
c |= ((_a & _b) | (c & (_a ^ _b))) ? mask: 0;
}
printf("%d\n", s);
return 1;
}