void printd(char []); int main(void){ char a[100]; a[0]=...">

Why is it not a mistake to increase the array "a" in the bottom 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 expected this to result in an lvalue error. But it works with some kind of error and gives bc as output. Why is this incrementing array "a" not an error?

+6
source share
5 answers

If the array is passed to the function, it splits into a pointer to the first element of the array.

Because of this, inside printd() pointer a can be increased and decreased to point to different elements of the array a , as defined in main() .

Note that when declaring / defining a list of function parameters for any type T expression T[] equivalent to T* .

In a specific case

 void printd(char a[]); 

coincides with

 void printd(char * a); 

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

 #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; `} 
+5
source

In declaring an array of C language in the list of function parameters and declaring the array outside the list of function parameters mean completely different things, even if they look the same (or the same) on the surface.

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

Meanwhile, your a in main is a true array.

+3
source

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

In C, arrays are passed as a pointer to any function. That is why you are not getting any errors.

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

Now, when you get the address of the first element a , a++ means the leading start address of array 1 . Therefore, the first printf prints b , the second element of the array a .

0
source

a also works as a pointer. This is a pointer to the first element of the array. A ++ increments this pointer. That is, after a ++ a [0] or * a gives you what it gave you [1] before the increment, that is, "b".

Note that you are not increasing the value [0] from 'a' to 'b'. Make sure that if you use different values ​​in your array, for example "a", "d", "h", "x".

0
source

The array is passed to the function as a pointer, which can be increased.

Thus, in your example, it is impossible to increase a inside the main() function, since a is an array and cannot be increased.

However, not following the syntax, a inside the printd() function is a pointer, so it can be increased. The increment a inside printd() does not affect the array a inside main () - although they have the same name, they represent different objects in different areas.

0
source

All Articles