Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

242
Visualizações
What is the assignment-expression in array brackets in C?

In C89 A.1.2.2 Declarations

direct-declarator [ constant-expression ]

In C99 A.2.2 Declarations

direct-declarator [ type-qualifier-list_opt assignment-expression_opt ]

I understand assignment-expression_opt as var = 1;. But arrays are not declared like int arr[i = 0];. Why does C99 use the term "assignment-expression" instead of "constant-expression"and what does it mean?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

In C89 there was no support for variable length arrays. This means that array sizes must be fixed at compile time.

Starting with C99, declarations and statements may be mixed within a block, so full expressions that must be executed are now allowed as an initializer. This also is what allows for variable length arrays to be created.

So a declaration like int arr[i=0]; is valid syntax, although it's invalid because it created an array of size 0. int arr[i=2]; is valid and will create arr as an array of int of size 2, and it sets i to 2.

over 4 years ago · Santiago Trujillo Relatório

0

This is because C99 supports VLA and whatever goes between [] may then be pretty much any expression.

As for why it says assignment-expression specifically:

The way the syntax of expressions and operator precedence is described in C, is by having each operator group's syntax point at the operator group with higher precedence than itself. For example look at the syntax for an assignment expression:

assignment-expression:
conditional-expression

It points at the conditional ?: operator which will be the next operator group with higher precedence than assignment operators.

And for the comma operator with lowest precedence of all, the syntax is actually:

expression:
assignment-expression
expression , assignment-expression

That is, the syntax for an expression in C. Now for some reason, C99 apparently didn't want to allow comma operators inside []. I don't know why - perhaps it would make qualified/static array parameter declarators too strange. So instead of using expression in the direct-declarator syntax, they picked the next operator group above the comma operator, the assignment operators.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda