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

304
Visualizações
Why it is not an error to increment array "a" in the below function?
#include<stdio.h>
void printd(char []);
int main(void){
       char a[100];
       a[0]='a';a[1]='b';a[2]='c';a[4]='d';
       printd(a);
       return 0;
}
void printd(char a[]){
        a++;
        printf("%c",*a);
        a++;
        printf("%c",*a);
}

Explanation: I was expecting that it would result in lvalue error. But it is working with out any error and giving bc as output. Why is this incrementing array "a" is not an error?

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

0

If an array is passed to a function it decays to a pointer to the array's first element.

Due to this inside printd() the pointer a can be incremented and decremented, to point to different elements of the array a as defined in main().

Please note that when declaring/defining a function's parameter list for any type T the expression T[] is equivaltent to T*.

In question's specific case

void printd(char a[]);

is the same as

void printd(char * a);

The code below shows equivalent behaviour as the OP's code, with pa behaving like a in side printd():

#include <stdio.h>

int main(void)
{
   char a[100];
   a[0]='a';a[1]='b';a[2]='c';a[4]='d';

   {
     char * pa = a;

     pa++;
     printf("%c", *pa);
     pa++;
     printf("%c", *pa);
   }

   return 0;
`}
over 4 years ago · Santiago Trujillo Relatório

0

In C language array declaration in function parameter list and array declaration outside of function parameter list mean completely different things, even though they look similar (or the same) on the surface.

When you use array declaration in function parameter list (as is the case with void printd(char a[]) in your code), you are not declaring an array. The top-level [] syntax in function parameter list is just an alternative form of pointer declaration. This means that your a parameter is actually declared as char *a. It is not an array at all, it is an ordinary pointer. There's nothing unusual in being able to increment such a, and this is why you are not getting any "lvalue errors" from it.

Meanwhile, your a in main is a true array.

over 4 years ago · Santiago Trujillo Relatório

0

Why is this incrementing array "a" is not an error?

In C arrays are passed as a pointer to any function. Thats why you get no error.

The function calls in main() pass the name of the array, a, as an argument because the name of an array in an expression evaluates to a pointer to the array. In other words, the expression, a, is a pointer to (the first element of) the array, a[]. Its type is, therefore, char *, and a called function uses this pointer (passed as an argument) to indirectly access the elements of the array.

Now as you receive the address of the first element of a, a++ means ahead the array's initial address by 1. That's why the first printf prints b, the second element of array a.

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