0

So the code looks like this (you can access this fiddle here (Click me):

#include <stdio.h>

int main()
{
    int a[5] = { 6, 2, 7, 3, 5 };

    for (int i = 0; i < 5; i++){
        printf("%d ", i[a]);
    }

    printf("\n");

    for (int i = 0; i < 5; i++){
        printf("%d ", a[i]);
    }

    return 0;
}

And this is the output:

6 2 7 3 5
6 2 7 3 5

Apparently, he made a mistake when indexing the array, and inverted the index variable with the array itself. What he found out is that it still prints the same values. This might be a silly question, but again, I don't know much about these types of cases, and this itches me to find out why it's happening what's happening here.

Thanks

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
Lucaci Andrei
  • 398
  • 9
  • 20

1 Answers1

3

x[y] is equivalent to *(x + y) which is equivalent to *(y + x) which is equivalent to y[x].

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115