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

296
Visualizações
Why isn't the compiler generating an error "lvalue required"?

As per my understanding, in the line marked as 'line 2' of the below code, the expression (*ptr)++ should generate "lvalue required" error because *ptr evaluates to a constant value of i=1, which is not lvalue?

So why is the program working successfully? Or am I somewhere wrong in my concepts? If yes, please enlighten me on this.

int main(void)
{
    int i;
    int *ptr = (int *) malloc(5 * sizeof(int));

    for (i=0; i<5; i++)
        *(ptr + i) = i;

    printf("%d ", *ptr++);   //line 1
    printf("%d ", (*ptr)++); //line 2 
    printf("%d ", *ptr);     //line 3
    printf("%d ", *++ptr);   //line 4
    printf("%d ", ++*ptr);   //line 5
}
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You're having a misconception. result of (*ptr) is lvalue, upon which the post increment operator can be applied.

So, in your case,

 printf("%d ", (*ptr)++); //line 2

is fine.

To quote C11 standard, chapter §6.5.3.2, Address and indirection operators, (emphasis mine)

The unary * operator denotes indirection. If the operand points to a function, the result is a function designator; if it points to an object, the result is an lvalue designating the object.

FWIW, if *ptr would not be a lvalue, you would have got error for writing something like *ptr = 5 also, wouldn't it?

over 4 years ago · Santiago Trujillo Relatório

0

What about int i = 0; i++;. If i is 0, then is 0++ valid? The answer is of course no, 0++ (and 1++) are not valid. ++ is not applied to the value, it's applied to the object (in this case i, or in your case, the object pointed to by *ptr).

over 4 years ago · Santiago Trujillo Relatório

0

An lvalue is something that refers to someplace in memory that can/does hold a value.So if *ptr=10; then *ptr is lvalue.

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