Normally in C++
, character arrays are initialized in the following way,
char example[5]="cat";
What if you initialize it with ""
(just a double quotes without spaces)?
What will be the elements in the character array after initialization?
char* is a pointer to a character. char is a character. A string is not a character. A string is a sequence of characters.
In order to declare a variable with character type, you use the char keyword followed by the variable name. The following example declares three char variables. char ch; char key, flag;. In this example, we initialize a character variable with a character literal.
Strings in C are represented by arrays of characters. The end of the string is marked with a special character, the null character , which is simply the character with the value 0.
In C++ we can do this task very easily using erase() and remove() function. The remove function takes the starting and ending address of the string, and a character that will be removed.
The declaration
char temp[3] = "";
is same as
char temp[3] = {0};
// `\0` ascii value is 0
remember remaining elements of half initialized array initialized with 0
.
Point :char temp[3] = ""
is easy to type(means writing), so its preferable.
Look even compare it with this declaration char temp[3] = {'\0'};
(it need more chars to type) Whereas in char temp[3] = "";
is simple (even no type mismatch - int/char).
It's a 3-character array initialized to three null characters.
EDIT (after comments below):
From K&R:
If there are fewer initializers for an array than the number specified, the missing elements will be zero for external, static, and automatic variables.
...
Character arrays are a special case of initialization; a string may be used instead of the braces and commas notation:
char pattern[] = "ould";
is a shorthand for the longer but equivalent
char pattern[] = { 'o', 'u', 'l', 'd', '\0' };
From a draft copy of the C++ standard, section 8.5.2, Character arrays:
"1. A char array (whether plain char, signed char, or unsigned char), char16_t array, char32_t array, or wchar_t array can be initialized by a narrow character literal, char16_t string literal, char32_t string literal, or wide string literal, respectively, or by an appropriately-typed string literal enclosed in braces. Successive characters of the value of the string literal initialize the elements of the array. [Example:
char msg[] = "Syntax error on line %s\n";
shows a character array whose members are initialized with a string-literal. Note that because ’\n’ is a single character and because a trailing ’\0’ is appended, sizeof(msg) is 25. — end example ]
...
"3. If there are fewer initializers than there are array elements, each element not explicitly initialized shall be zero-initialized (8.5)."
A blank string. The first char will be the null terminator. After an experiment, it appears the remaining characters are set to 0 (which is also null terminator).
You are setting the first element to be a null termination character. The other elements gain partial initialisation to zero: C and C++ : Partial initialization of automatic structure
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