Are the characters [4] and 4 [chars] the same in C? What for?

I read it and don’t believe it. I do not have a compiler for testing.

+3
source share
3 answers

In raw C, a designation []is just a helper pointer helper. Before []you have to look for the fourth char in the block pointed to by ptr, for example:

*(ptr+4)

Then they entered a shortcut that looked better:

ptr[4]

What transalts to an earlier expression. But, if you write it like this:

4[ptr]

This will translate to:

*(4+ptr)

This is really the same.

+9
source

Since a [b] exactly matches * (a + b) and + commutes.

chars[4]- *(chars+4), and 4[chars]-*(4+chars)

+5
source

Source: https://habr.com/ru/post/1723557/


All Articles