# Need help to understand x["AC"] in the following code

• 06-05-2006
jcourcel
Need help to understand x["AC"] in the following code
Hello

I just start to learn C. Is someone could help me to understand the x["AC"] on the line putchar( x["AC"] ); This is the only one line I do not understand in this code. Thanks a lot for help.

Code:

```#include <stdio.h> int main() {     int x;           for ( x = 0; x < 2; x++) {       putchar( "AC"[x] );       putchar( x["AC"] );       putchar( *("AC" + x) );       putchar( *"AC" + x );       putchar('\n');     }     return 0; }```
• 06-05-2006
bivhitscar
[EDIT]

And I've never seen arrays used like that, so I'll have to leave it to someone else to explain.
• 06-05-2006
whiteflags
I am very surprised if this compiles because "AC" is not exactly an internal constant. That said, all four statements do exactly the same thing: dereference a particular cell of memory.
Code:

```int x[] = { 0, 1, 2, 3 }; printf("%d ", x[0]); printf("%d ", 1[x]); printf("%d ", *(x + 2));```
C is very flexible in that x[3] dereferences the same as 3[x], and same as *(x + 3)
• 06-05-2006
Prelude
"AC"[x] is the same as *("AC" + x) which is the same as *(x + "AC"), which is the same as x["AC"] because addition is commutative.
• 06-05-2006
bivhitscar
>C is very flexible in that x[3] dereferences the same as 3[x]

So, you can switch the index with the name and it is valid?

I don't like it... :P
• 06-05-2006
jcourcel
Thanks a lot citizen. I did not know about the 3[x] notation.
• 06-05-2006
whiteflags
> I don't like it... :P
People who obfuscate code know this about you. ;)
• 06-05-2006
Prelude
>So, you can switch the index with the name and it is valid?
Yes and no. As long as the resulting syntax is valid and the behavior is identical, it works. But take a[x * y]. For the switch to work, you need to surround the expression in parens: (x * y)[a].
• 06-05-2006
bivhitscar
Quote:

Originally Posted by Prelude
Yes and no. As long as the resulting syntax is valid and the behavior is identical, it works. But take a[x * y]. For the switch to work, you need to surround the expression in parens: (x * y)[a].

Amazing, I had no idea. Is there any practical use for this?
• 06-05-2006
Prelude
>Is there any practical use for this?
No, it's just some minor trivia that fell out of how arrays are parsed.
• 06-05-2006
bivhitscar
Fair enough. :P
• 06-06-2006
Salem