Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

308
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda