In the C language, the following rules apply (some of them don't apply to others like C++):
- Arrays may degrade to pointers, possibly lossing their constant properties as described in rule 4.
- Any expression of the form
x[y]
is equivalent to *(x + y)
.
- A string literal represents a null-terminated array of characters.
- A variable name
a
for a given array is constant, and is equivalent to &a[0]
.
- For any non-constant, non-volatile type
T
, an expression of type T
may be assigned to a variable name whose type may or not include the constant and/o volatile qualifiers, and if non-qualified would have type T
, but an expression that constant and/or volatile qualified, and has type T
when non-qualified, may not be assigned to a variable name that lacks the qualifiers of such an expression.
This implies that all the following assignments all valid:
char str[] = "Hello, world!\n"
, due to rule 3.
const char str[] = "Hello, world!\n"
, due to rules 3 and 5.
volatile char str[] = "Hello, world!\n"
, due to rules 3 and 5.
const volatile char str[] = "Hello, world!\n"
, due to rules 3 and 5.
char *str = "Hello, world!\n"
, due to rules 3 and 1.
const char *str = "Hello, world!\n"
, due to rules 3, 1, and 5.
volatile char *str = "Hello, world!\n"
, due to rules 3, 1, and 5.
const volatile char *str = "Hello, world!\n"
, due to rules 3, 1, and 5.
Given rules 4 and 5, a call to void fit(char*, int)
shall fail if the expression assigned to the first argument is non-qualified, such as if given the statement const char *mesg = "Hey, Lisa. You look so beautiful!";
, the variable name mesg
is assigned to the first argument in a call to void fit(char*, int)
.
Just for completeness, rule 1 is strongered by rule 2, as seen in the expression *(p + n) = '\0'
whenever p
was degraded from an array at its assignment.
tl;dr: Most differences between both of the situations you described occur whenever a mismatch of qualifiers occur, as described in rule 5 above. This is mostly due to "historical reasons" (i.e: lazyness), and has affected other languages. For example, C++ saves some "compatibility with C", or in other words, compatibility with lazyness.