Is there any programming reason why I always see function definitions with arguments like this:
char getchar(char *arr){
Instead of this?:
char getchar(char arr[]){
These are identical. It is a pointer in both cases. So I guess it may be more clear to write it as a pointer. The second case might cause beginners to think that you are passing an entire array by value or something like that.
Also this code:
const char anonymous[ ] = "123"; char *arr = anonymous;
Didn't work for me, it gives a " invalid conversion from `const char*' to `char*'" compile error.
Yes. Either "arr" needs be changed to be declared as "const char *", or "anonymous" needs be changed to be declared as "char []".
Would it be correct to say that char *arr = "123"; is like this to the compiler:
char anonymous[ ] = "123"; char *arr = anonymous;
Without the constant declaration?
No. There are two special syntaxes here. But they are very different. The line 'char *arr = "123" ' makes a string literal (which is stored specially in some special part of memory by the compiler, and exists for the entire duration of the program) and gives the location of that to the pointer. The second code example creates an array which is a local variable of the current function, and initializes it. As a local variable, it only exists until the function returns, so it would be bad to return a pointer to it or refer it …