c - 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: expecting result in lvalue error. working out error , giving bc output. why incrementing array "a" not error?

if array passed function decays pointer array's first element.

due inside printd() pointer a can incremented , decremented, point different elements of array a defined in main().

please note when declaring/defining function's parameter list type t expression t[] equivaltent t*.

in question's specific case

void printd(char a[]); 

is same as

void printd(char * a); 

the code below shows equivalent behaviour op's code, pa behaving 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; `} 

Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -