char arr[] = "asds"
Here, arr
is just a name. It refers to a memory location but is not a pointer. The compiler substitutes the address directly wherever arr
is used. It is not a pointer because unlike pointers, it does not have any space allocated to store an address. It is a mere compile time symbol. Hence, at run-time there is nothing on which you can do pointer arithmetic on. If you had to increment something, that something should exist at run-time.
More details:
Basically, the literal "asds" is stored in your executable and the compiler knows where exactly it is (well, the compiler is placing it in the executable, so it should know?).
The identifier arr
is just a name to that location. As in, arr
is not a pointer, i.e: it does not exist in memory storing an address.
void func(char arr[])
In case of a function argument, the arr
does exist in memory at run-time because the arguments are pushed onto the call stack before making the function call. Since arrays are passed by reference, the address of the first element of the actual parameter is pushed onto the call stack.
Therefore, arr
is allocated some space on the stack where it stores the address to the first element of your actual array.
Now you have a pointer. Hence you can increment (or do any pointer arithmetic on it).