2

I am trying to solve this enigma:

int direction = 1;
char direction_name = direction["nsew"];

Like why does it compile on a first place, what exactly is it trying to prove and what is it used for ?


I spotted this code in a site with no relevant explanation other than "[] is symmetric". I mean what am I supposed to do with this information..

Imobilis
  • 1,475
  • 8
  • 29
  • for any array or pointer `p`, `i[p]` and `p[i]` come to share the same meaning, i.e. `*(p + i)`. the first form is perfectly valid and you can find it in standard. but indeed it's too bazaar so you can see it rarely. – Jason Hu Aug 14 '15 at 18:34

2 Answers2

4

This code is equivalent to:

#include <stdio.h>

int main(void) {
    char direction_name = 1["nsew"];
    printf("[%c]\n",direction_name);
    return 0;
}

which will print [s], because

char direction_name = 1["nsew"]; // "nsew" is string literal, i.e. an array
                                 // of 5 chars 'n' 's' 'e' 'w' '\0'

is equal to

char direction_name = "nsew"[1];

and also to

char direction_name = *("nsew" +1);

Therefore [] is symmetric in a sense that

x[y] == y[x] // if conditions given in C standard 6.5.2.1/2 are met

You can think of [] as an symmetric relation in algebra (under assumptions from 6.5.2.1 § 2):

enter image description here

Or you may think about [] as a linear mapping (transformation, functional) between reflexive (Banach) linear spaces V and V** if you wish:

x[f] = [f,x] = f[x]

C standard n1124 6.5.2.1 § 2 Array subscripting (emphasis main)

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))). Because of the conversion rules that apply to the binary + operator, if E1 is an array object (equivalently, a pointer to the initial element of an array object) and E2 is an integer, E1[E2] designates the E2-th element of E1 (counting from zero).

4pie0
  • 29,204
  • 9
  • 82
  • 118
3

The following statement:

char direction_name = direction["nsew"];

is equivalent to:

char direction_name = "nsew"[direction];

as well as to:

char direction_name = *("nsew" + direction); // note that addition is commutative

The direction holds integer value 1, hence you are getting second letter (in C arrays are indexed from zero) of string literal "nsew" (it is of array type char[5]), that is 's' character.

Grzegorz Szpetkowski
  • 36,988
  • 6
  • 90
  • 137
  • Can you also explain in what sense they are "symmetric". An orange and a banana cannot be the same thing without an appropriate explanation. Like.. `printf("%i", "asdf"[1] == 1["asdf"]);` means symmetric – Imobilis Aug 14 '15 at 18:26
  • 1
    @Malina: They are symmetric, because additiion is commutative. The expression `a[b]` is the same as `*(a + b)` using pointer notation, which is equivalent to `*(b + a)`, which implies `b[a]`. – Grzegorz Szpetkowski Aug 14 '15 at 18:33