Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is meant by char temp[3]="";?

Tags:

c++

arrays

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?

like image 516
chinthana Avatar asked Jul 22 '13 14:07

chinthana


People also ask

Is char * a string?

char* is a pointer to a character. char is a character. A string is not a character. A string is a sequence of characters.

How do you declare a character in C?

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.

What is at the end of a string in C?

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.

How do you clear a character in C++?

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.


4 Answers

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).

like image 76
Grijesh Chauhan Avatar answered Nov 15 '22 06:11

Grijesh Chauhan


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)."

like image 30
Andy Thomas Avatar answered Nov 15 '22 07:11

Andy Thomas


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).

like image 39
Neil Kirk Avatar answered Nov 15 '22 07:11

Neil Kirk


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

like image 38
TheDarkKnight Avatar answered Nov 15 '22 08:11

TheDarkKnight