-6

The following code seems to execute fine but how is it valid and whats happening here?

    int i;
    printf("%d",i["11"]);
manojadams
  • 2,314
  • 3
  • 26
  • 30
  • 8
    UB: `i` is an uninitialized array index. – nneonneo Aug 20 '13 at 05:20
  • 5
    There are many questions that this is a duplicate of. The difficulty is going to be finding them. – Jonathan Leffler Aug 20 '13 at 05:22
  • 3
    What is with C programmers nowadays? The first thing they are trying to learn is how to abuse the system. – Krishnabhadra Aug 20 '13 at 05:24
  • What is the output..? @manoj – Kira Aug 20 '13 at 05:24
  • 3
    @JonathanLeffler Here is one of the dupes http://stackoverflow.com/questions/381542/in-c-arrays-why-is-this-true-a5-5a -- the question got 550 upvotes. – Ray Toal Aug 20 '13 at 05:28
  • 1
    possible duplicate of [Why does i\[arr\] work as well as arr\[i\] in C with larger data types?](http://stackoverflow.com/questions/7181504/why-does-iarr-work-as-well-as-arri-in-c-with-larger-data-types) – nneonneo Aug 20 '13 at 05:28
  • Thanks Ray and nneonneo. Either of the suggested duplicates would be a better way of closing this question than 'off topic'. However, as long as it is closed in some way, I'm not going to get too fussed. – Jonathan Leffler Aug 20 '13 at 05:30

2 Answers2

7

i["11"] == *("11" + i) == "11"[i]

nneonneo
  • 171,345
  • 36
  • 312
  • 383
1

when you do i["11"] what you do is say to the compiler to take the value of i and add to it the value of "11" and take the value on the address that is the sum of them

No Idea For Name
  • 11,411
  • 10
  • 42
  • 70