In C,  a[5] == 5[a]

Yeah!!! Looks funny but true.

In C array indexing is actually defined as

a[i] = *(a+i)

You can also make this as

a[i] = *(i+a)

As from our childhood math, we know, a+i and i+a are equal.

As pointers are memory addressing, a[i] that is defined as *(a+i) is address of i elements further from address of “a”. This is also equal to “a” from i elements from beginning of address space (i+a).

And yes

"ABCD"[2] == 2["ABCD"] == 'C'

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s