Braces around string literal in char array declaration valid? (e.g. char s[] = {Hello World})
It's allowed because the standard says so: C99 section 6.7.8, §14:
An array of character type may be initialized by a character string literal, optionally
enclosed in braces. Successive characters of the character string literal (including the
terminating null character if there is room or if the array is of unknown size) initialize the
elements of the array.
What this means is that both
char s[] = { "Hello World" };
and
char s[] = "Hello World";
are nothing more than syntactic sugar for
char s[] = { 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', 0 };
On a related note (same section, §11), C also allows braces around scalar initializers like
int foo = { 42 };
which, incidentally, fits nicely with the syntax for compound literals
(int){ 42 }
C++, char* string modification
why *str = 'S' crashes the program?
Because you're not allowed to modify string literals. The C++ standard allows them to be stored in read-only memory.
In fact, if you enable compiler warnings, you get:
prog.cc:5:16: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
char* str = "Test";
^~~~~~
Always use const char*
when pointing to string literals:
In C, is array initialization with only one element treated specially?
char array[] = {"s"};
is same as:
char array[] = "s";
Here { }
are optional in this case because "s" is string literal.
Or,
char array[] = {'s', '\0'};
In this case, { }
are necessary to initialize the array.
Difference between two methods of array pointer initialization
ISO 9899-1990 6.5.7 ("Initialization") says :
An array of character type may be initialized by a character string
literal, optionally enclosed in braces.
What is the difference between char s[] and char *s?
The difference here is that
char *s = "Hello world";
will place "Hello world"
in the read-only parts of the memory, and making s
a pointer to that makes any writing operation on this memory illegal.
While doing:
char s[] = "Hello world";
puts the literal string in read-only memory and copies the string to newly allocated memory on the stack. Thus making
s[0] = 'J';
legal.
Attempting to write a String constructor to handle static char * such as Hello World
Simplest would be to take const char*
as the parameter. Then use strlen
to find the string length, then allocate len+1
characters using new
and use strncpy
to copy the string to the newly allocated memory. BTW, any specific reason not to use std::string
?
Related Topics
Unique_Ptr and Openssl's Stack_Of(X509)*
Using << Operator to Write to Both a File and Cout
C++ Regex for Overlapping Matches
Aligning Static String Literals
The Most Efficient Way to Reverse a Number
C++: Where Does the Ofstream Class Save the Files To
Qt 5 and Qprocess Redirect Stdout with Signal/Slot Readyread
Calculating the Info-Hash of a Torrent File
How to Compile C++11 Code with Orwell Dev-C++
Reading JSON File with C++ and Boost
Are the "Usual Arithmetic Conversions" and the "Integer Promotions" the Same Thing
Referencing Memory Operands in .Intel_Syntax Gnu C Inline Assembly
When Should I Use the Keyword "Typename" When Using Templates
Qsqldatabase: Qmysql Driver Not Loaded on Ubuntu 15.04 64Bits