"Operator" brackets [] in creating the arrangement in C

7

Whenever I look for what the brackets operator [] does, even in the tables that show all operators in C, it appears that it serves to access an element of an array. However, this is not always the case, for example in the case where the arrangement does not yet exist, that is, at creation time:

int main()
{
    // nessa linha, eu tenho a certeza de que o colchetes não faz acesso a nada.
    void *ptrArray[100];

    // porém, nessa linha, eu sei que o colchetes faz o acesso ao 2º elemento do arranjo
    ptrArray[1];
}

So since there's no mention anywhere about the operator [] at the time of creating arrangements, I assume he's not an operator in this case. Is that right?

How does the compiler differentiate? I believe the compiler recognizes the difference because it saw that there was a keyword of a primitive type or struct before, in my case it is the keyword void .

The return of the [] operator is always a pointer, right? So, at compile time, it's like void *ptrArray[100] was void **ptrArray , right?

    
asked by anonymous 12.09.2018 / 16:20

2 answers

7

In this case it is not an operator, it is an identical syntax with different semantics. In this line it is not operating anything, it is only declaring.

The compiler differentiates by the context where it is. When it appears in what we call lvalue it is always the form of declaration. When it appears in rvalue is the operator.

Lvalue is the declaration itself, in this context it is what comes before = , and rvalue is an expression that can be used in several places of the code. This is not to say that it can be used at all 'lvalue'. In fact needs to be in a statement, this is already a semantic evaluation, is another step of the compilation. If you want to know more: How is a compiler done? .

Obviously this is a simplification, the exact rule is much more complicated and can range from compiler to compiler, as long as it complies with everything the specification dictates.

[] is always a way of accessing an address by a pointer. And void *ptrArray[100] is like a void **ptrArray statement, but it's not the same thing. See more in Arrays are pointers? . You have multiple links about this .

    
12.09.2018 / 16:24
5

I will use the most recent draft N2176 (which I found here: link ) for C standardization to respond.

In the [6.7.6] , one of the parts of a declarator syntax is the direct declaration (or direct-declarator ), which is where the syntax for the declaration of arrays is. What we are interested in here is the array declarator: Given a T D1 statement, if D1 is in one of the following ways:

   opt assignment-expression opt ]

     

D type-qualifier-list opt assignment-expression

     

D type-qualifier-list static assignment-expression

     

type-qualifier-list opt

So we have an arrangement declarator. Note that the square brackets are used only to indicate an arrangement declarator, nothing else. There is no 1 subscript operation occurring here. For example, in the int a[8] statement, the a[8] part is an array declaration, where 8 is the size of the array (or number of elements).

If we are not in the declaration context and find an expression in the form I[N] , where I has already been declared as an array (or pointer) then surely we have the subscript operation occurring. For example:

int a[8]; // Declara 'a' como um arranjo de 8 elementos de tipo 'int'.
a[0]; // Acessa o primeiro elemento de 'a', retornando um valor do tipo 'int'.

Finally, about his last question regarding the equivalence between arrangements and pointers: arrangements may fall to pointers. Preparing: In the int a[8] statement, the identifier a was declared to have type int[8] . However, the expression a may well result in a value whose type is a pointer.

void foo(int *);

int main()
{
    int a[8]; // Declara 'a' tendo tipo 'int[8]'.

    foo(a); // O valor que a expressão 'a' em si gera terá o
            // tipo decaído para um 'int *'.
}

This decay behavior is described in the [ 6.3.2 / 3] :

  

Except when it is the operand of the sizeof operator, or the unary & operator, or is a literal string used to initialize an array, an expression that has type "array of type" is converted to an expression with type "pointer to type" that points to the initial element of the array object and is not anvalue. [...]

1 Subscript is the name of the element access operation of an array.

    
12.09.2018 / 20:34