0

At first it was found by mistake but I like this feature of C very interesting. a simple program like this :

#include <stdio.h>
#include <string.h>

main()
{
   int array[5];
   array[0] = 1;
   printf("%d",0[array]);
}

will work with no compile error in C and it was not something that I expect. And it will print 1 in the out put. I found this amazing so I just wondering around and found this code will work fine in C too and this will print 10 in the output:

#include <stdio.h>
#include <string.h>

main()
{
   int array[5];
   array[0] = 1;
   array[1] = 10;
   printf("%d",0[array+1]);
}

I want to know why these two have no compilation error and also I want to know how the second one work at all. Also any interesting usage of this feature will be appreciated.

Lrrr
  • 4,755
  • 5
  • 41
  • 63
  • 1
    It's yet another manifestation of the fact that in C arrays don't really exist. – Hot Licks Sep 24 '14 at 23:51
  • @Hot Licks: Not really. Arrays "didn't really exist" in B language. C arrays are different, but their interface specification was intentionally made compatible with B, which is why it might appear sometimes that in C arrays "don't really exist" either. This is an illusion though. Arrays do exist in C. – AnT stands with Russia Sep 24 '14 at 23:53
  • @HotLicks what is that means? I'm not a C programmer, so I didnt know that much about what you said, could you provide me some links to understand it better? – Lrrr Sep 24 '14 at 23:54
  • 1
    @AliAmiri - Basically, except for declarations and a few odd situations an array is treated like a pointer, and, in particular, once an "array" is passed to another method it's indistinguishable from a pointer. Arrays are basically just "syntactic sugar". – Hot Licks Sep 24 '14 at 23:59

2 Answers2

3

The behavior you observe is according to the standard:

6.5.2.1 Array subscripting

A postfix expression followed by an expression in square brackets [] is a subscripted designation of an element of an array object. The definition of the subscript operator [] is that E1[E2] is identical to (*((E1)+(E2))).

So, in your example, the 0[array] becomes (*(0 + array)), and 0[array+1] becomes (*(0 + array + 1)). These can then be reduced to *array and *(array+1), respectively, both of which are valid statements.

Community
  • 1
  • 1
AlexD
  • 32,156
  • 3
  • 71
  • 65
1

The square-bracket array syntax is an easy-to-use way of handling memory pointers to sequentially allocated memory blocks.

What you've discovered is an artifact of the way pointer arithmetic works in C. Every variable is stored at a particular address in memory, and arrays are just several variables in a row in memory (as far as the code is concerned). So, the name of your array is the pointer to the first item in memory, while the index is the offset. The compiler adds these together to get the final address, so it doesn't really matter in which order you list them.

In C, the following lines of code will output the same thing three times:

int values[] = {0, 1, 2};
printf( "%d %d %d", values[1], 1[values], *(values + 1) );

The third one is the manual way of accessing the memory address. We start with a pointer to the first byte in the array and add an offset for the index.

Woodrow Barlow
  • 8,477
  • 3
  • 48
  • 86