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

This is a discussion on Need help to understand x["AC"] in the following code within the C Programming forums, part of the General Programming Boards category; Hello I just start to learn C. Is someone could help me to understand the x["AC"] on the line putchar( ...

1. ## 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;
}```

2. [EDIT]

And I've never seen arrays used like that, so I'll have to leave it to someone else to explain.

3. 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)

4. "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.

5. >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

6. Thanks a lot citizen. I did not know about the 3[x] notation.

7. > I don't like it... :P
People who obfuscate code know this about you.

8. >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].

9. 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?

10. >Is there any practical use for this?
No, it's just some minor trivia that fell out of how arrays are parsed.

11. Fair enough. :P