### Pointers and Arrays - Basic question

Hi I'm stuck wondering how an array assignment is legal.

num[i] equals *( num + i) equals *(i + num ) equals i[num]

It's the last assignment I'm unsure about, how can example 1[num] equals num[1]?
There is some grave lacking in my understanding about the C language, perhaps this is quite known logic?
Can somebody give me a reference to read up on when it comes to this specifically?
Where did you learn it's equal? num[i] and i[num] are two very different things, starting from the fact that the first refers to an array called num and the second to an array called i.
No no, i is not an array, it's simply an integer variable.
You can even forget the variable..

num[1] = 1[num] this is what boggles my mind..

For example the compiler internally converts num[1] into *(num+1)
Last edited on
Oh yeah right. The square brackets are syntactic sugar for *(address + offset). So doing i[num] is like saying *(offset + address) which leads to dereferencing the same memory address as the first one.
Ah, thank you very much. Seems a lot clearer now! :)
While I have the thread, do you know any C books which explains such topics?
Last edited on
I don't know any book, I studied using the Kernighan&Ritchie but I didn't find it much newbie friendly. You can try googling 'pointer arithmetics' to get some more info on the subject.
Ok, thank you for your help. I needed it ;)
Last edited on
Topic archived. No new replies allowed.