Is there any way that I can add const keyword to an array passed as a parameter to function:
void foo(char arr_arg[])
If I place const before char (void foo(const char arr_arg[])) or after char(void foo(char const arr_arg[])), that would mean than it’s char which is constant, not the arr_arg.
I have just read that under the hood an array sent as a parameter to function is represented as a pointer, so void foo(char arr_arg[]) is the same as void foo(char* ptr_arg).
Taking it into account, I may rewrite the function as void foo(char * const ptr_arg) for it to be exactly what I want to achieve.
But I want to know if there is a way to add const keyword in this declaration void foo(char arr_arg[]) for it to be the same as void foo(char * const ptr_arg) (and not void foo(char const * ptr_arg) or void foo(const char * ptr_arg))?
I just want to understand if there is a syntax to make arr_arg constant with array notation [].
In C you have to put
constbetween the[], however strange that might look to an unprepared personThis is “new” C99-specific syntax. In C89/90 or C++ there no way to do it with “array” syntax, so you have to switch to the equivalent “pointer” syntax, as suggested in David’s answer.