1

I have an array declared like this

float dati[10];

I normally subscript the array like as follows

dati[2] = 5;

But i have also seen other types of substriptions like

2[dati] = 5;

and

*(dati+2) = 5;

Can anyone tell me what's the difference between these three?

Baz
  • 143
  • 3
  • 9

1 Answers1

3

Compiler translates a[i] as *(a+i), so this bit links up your first and third manner of indexing.

Now, a being the address of first element of an array - aka a pointer - and i being an integral constant or variable you know a + i is the same thing as i + a. This means the following also holds

a[2] = *(a + 2) = *(2 + a) = 2[a]

So all three are equivalent ways of accessing the third element of an array.

Tanveer Badar
  • 5,438
  • 2
  • 27
  • 32