I found in the supported code an example of accessing the array n [a], where n is the index and a is the array. In a simple example, it looks like

#include <stdio.h> void main() { char hello[] = "hello"; puts(&0[hello]); } 

This line & 0 [hello] - what does it mean?

    5 answers 5

    Standard C explicitly allows such addressing.

    6.5.2.1 Array subscripting

    <...>
    This is a subscripted object.
    The definition of the subscript operator [] is that E1[E2] is identical to (*((E1)+(E2))) .
    <...>

    For what it is made difficult to say. Most likely, they wanted to simplify the addressing as much as possible, respectively, they didn’t bother with complex structures and defined the operation of obtaining the array element as a dereference of the sum. Since the “plus sign” is commutative, as a result of the desire to simplify, such a curiosity has turned out.

    I can not think of a single case when such a "coup" of operands would be justified.

      as far as I remember, this is equivalent to & hello [0]. just someone show off. This is passing the address of the 0th element of the array (char string) to the puts function. Further reference:

      int puts (const char * str);

      Write a String to Stdout and a Newline Character.

      • Why is this so? Why does C allow this syntax? - stanislav
      • Well, just ... they wanted to do it. maybe it seemed convenient to someone .. I don’t think there was any technical need for this. - alphard
      • 3
        Because 1 [hello] = (1 + hello) and hello [1] = (hello + 1) are the same. Imagine that 1 is not a constant, but a variable. Meaning to prohibit one of the options? With the same success, you can even ban the operator []. And force the user to use pointer arithmetic: hello [1] = x; equivalent to * (hello + 1) = x; - gecube 4:27 pm
      • "Imagine that 1 is not a constant, but a variable." - introduced. I still do not understand why such a syntax is needed. - dzhioev

      Just the element address is calculated by adding the index and the address of the first element.

      C allows a similar syntax. UPD : erased garbage.

      • If the index and address simply add up, then what if elements are larger than a byte? Then you need to multiply the index by the size of the element. In the passage below, there is no multiplication by sizeof. It means that it will most likely work only for single-byte ones. I compile, I will check. - Vladimir Gordeev
      • Yes, my guess is wrong. - Vladimir Gordeev
      • Hard trash. And how would it seem that a banal * (hello + 3) would work? Finally, the operation of adding a constant to a pointer was not made up by fools ... - kirelagin
      • fie damn, here the size of the pointer type is taken into account, exactly. - Vladimir Gordeev
       &0[hello] = &(*(0+hello)) = 0+hello (операция & обратна операции *) = hello 

      Total it was possible to write not puts (& 0 [hello]); and just puts (hello);

        Everything is very simple. As we know, the compiler sees hello[0] differently. He interprets this in *(hello + 0) . This means that such a &hello[0] entry would look like &(*(hello + 0)) .

        The sum does not change from permutation of the terms, it means *(hello + 0) == *(0 + hello) and &(*(hello + 0)) == &(*(0 + hello))


        Go ahead *(hello + 0) == hello[0] and *(0 + hello) == 0[hello]


        &hello[0] == &(*(data+0)) and &0[hello] == &(*(0+data))


        Well, this is the address of the first element that is &0[hello] == hello

        • one
          If you refer to the general rule about permutation of the addends, you implicitly assume that hello + 0 calculated as “address of hello plus 0”. And for the case of hello + 1 there would be “the address of hello plus 1”. This is not the case when the type of the array element is not single byte. The correct formula is &hello[i] = &hello + i * sizeof(T) , where T is the element type of the array. In such a formulation, it is not possible to talk about any rearrangement of the terms. - VladD
        • @VladD, or I did not understand something, or you wrote a lie. True formula &hello[i] == hello + i . The compiler itself "multiplies" i by sizeof (T) to calculate the address. - dzhioev
        • @dzhioev: In this sense, yes. My calculation refers to specific numbers, addresses. See, if the compiler in the hello + i expression multiplies the second term by sizeof(T) , we get hello + i * sizeof(T) in the sense of physical addresses. If you rearrange the terms ( i + hello ), it turns out in the sense of the physical addresses i + hello * sizeof(hello) (we multiplied by the size of the second term), right? And this is obvious nonsense. - VladD