What is the difference between the following?
char input[] = {"abc"};
and
char input[] = "abc";
A more convenient way to initialize a C string is to initialize it through character array: char char_array[] = "Look Here"; This is same as initializing it as follows: char char_array[] = { 'L', 'o', 'o', 'k', ' ', 'H', 'e', 'r', 'e', '\0' };
In general, C strings are mutable.
The behavior is undefined if a program attempts to modify any portion of a string literal. Modifying a string literal frequently results in an access violation because string literals are typically stored in read-only memory.
Both forms are equivalent and permitted.
char input[] = "abc";
or
char input[] = {"abc"};
Here is the relevant paragraph from the C Standard:
(C99, 6.7.8p14): "An array of character type may be initialized by a character string literal, optionally enclosed in braces"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With