It makes no sense to mark it as const, as you suggest. In C, function arguments are passed as a copy. This means that the dest
variable inside strcpy()
actually a new variable (pushed onto the stack) that contains the same content (here, the address).
Take a look at this function prototype:
void foo(int const a);
This has no semantic meaning, because we know that the original variable a, which we passed to foo()
, cannot be changed, because it is a copy. Only a copy will potentially change. When returning foo, we guarantee that the original variable a
does not change.
In the parameters of the function, you want to use the const keyword only if the function can really constantly change the state of the variable. For instance:
size_t strlen(const char *s);
This means that the contents of the variable s
(i.e. the value stored at address s
points to) as const. Therefore, it is guaranteed that your string will be immutable when the string returns.
source share