C Program not C++, but pretty easy i guess

main()
{
  int b[]={10,20,30,40,50};
  int i;
  for(i=0;i<=4;i++)
    printf(ā€œ%dā€,i[b]);
}


output is: 1020304050
but how? Shouldn't the output be this when its b[i] and not i[b] ?
Both work, but I can't remember why. It's still not very useful to use though. Very difficult to read.
The output is absolutely correct
b[i] is equivalent to *(b+i)
i[b] is also equivalent to *(i+b)
Hence b[i] and i[b] will produce the same output
hey thanks for reply,
if i am right, b[i] is understood by compiler as, *(address of b + i (into)size of each element of b) right?

so wont i[b] be *(address of i + b*size of i) ... but that doesn't make any sense, does it?

so i guess i am wrong in my understanding of b[i] ... please correct me ...

*(b+i) ... i know what * does, but how is the (b+i) part calculated?
Why would i[b] be *(address of i +...)? You don't take the address of i do you?

The types of b and i are independent of the place they hold in the expression.
What happens is, that the compiler adds two numbers and returns the adress of that piece of memory. b is some number like 0x33ff99 (a whole bunch of zero and ones) and the other one is 0x000000 through 0x000004. With printf you pick that piece of memory and print its value
Topic archived. No new replies allowed.