From what I learned, when we want to call "some_function" - we should pass an to the argument using the &
operator.
Note that in the case of arrays, you should omit the &
operator since an array decays to a pointer to its first element when passed as argument to a function.
But I've seen working examples where "some_function" directly receives a string:
some_function("some string");
How does it work? Is this practice recommended?
A string literal is basically stored in an not modifiable char
array (likely but not necessary in read-only memory).
When you use a string literal like that which is is indicated by its surrounding "
s, it decays to a pointer to its first element, which is of type char *
.
So, There is no problem using this technique.
Note that you can't modify a string literal, so any attempt to change the contents of "some string"
inside of `some_function invokes undefined behavior.