What is the best way to initialize an array of characters using a string literal?

I am new to C language. I am trying to understand the concept of an array in C. I have a confusion about initializing an array.

What is the best way to initialize a character array using a string literal?

 char arr[3] = "xyz"; 

or

 char arr[] = "xyz"; 

Thanks in advance.

+5
source share
4 answers

If in special cases the second method is always preferred, i.e. Do not type the size of the array explicitly. This avoids the error that you seemingly quietly created in your example.

To understand this, you must first understand what a string is. The null character is indicated by the character '\0' . A string is a sequence of zero or more non-zero char s ending with one null character. This last bit is very important. Take a look at the following code:

 const char* my_string = "xyz"; size_t string_len = strlen( my_string ); // string_Len == 3 

A pointer is just a memory address. It does not contain any information about size or length. Then, how strlen() measure the length of my_string ? This, of course, is by measuring the number of non-zero characters from a line starting immediately before the terminating null character. You may have noticed that the terminating null character is implicit in the string literal. The above string literal creates an array in memory that looks like this:

  _______ _______ _______ _______ | | | | | | 'x' | 'y' | 'z' | '\0' | |_______|_______|_______|_______| ^ | `my_string` is a pointer to this cell 

The array itself has no name, but the compiler manages to assign its first element address as my_string value. So what happens to your first example?

 char my_string[ 3 ] = "abc"; 

In the standard definition, a string literal is of type char[ N ] , where N is the length of the string plus one to count the null character (note that string literals are not declared const for historical reasons, but it is still undefined to change them). Thus, the above expression "abc" is of type char[ 4 ] . my_string , on the other hand (now it is an array, not a pointer, BTW) is of type char[ 3 ] . That is, you are installing a smaller array for a larger array, since 4 > 3 . The standard provides that in this exact situation, when the null character of a string literal does not fit into an array, it will be truncated. So my_string looks like this:

  _______ _______ _______ | | | | | 'a' | 'b' | 'c' | |_______|_______|_______| 

It looks fine, but ... wait. Where does the null character end? You chopped it off by explicitly declaring the size of the array! Now how should strlen() determine the length of a string? It will simply continue reading characters line by line until a null character is found by coincidence. This behavior is undefined. On the other hand, doing this:

 const char[] my_string = "abc"; 

You do not risk doing this. The type my_string will be automatically my_string to const char[ 4 ] , and the null character will be saved.

tl; dr Do not forget about the terminating null character!

+7
source

Whenever you initialize an array of characters using a string literal, do not specify a restriction on the string initialized by the string literal, because the compiler automatically allocates enough space for the entire string literal, including the terminating null character.

C standard (c11 - 6.7.8: clause 14) states:

A character type array can be initialized with a character string literal or UTF-8 string literal, optionally enclosed in curly braces. Sequential bytes of a string literal (including a terminating null character if there is space or an array of unknown size) initialize the elements of the array.

 char arr[3] = "xyz"; 

In this example, the size of arr is 3 , but the size of the string literal is 4 . the string defines one more character (ending '\ 0'), which the array may contain.

 char arr[] = "xyz"; 

This example does not specify the boundary of the character array in the array initialization. If the boundary of the array is omitted, the compiler allocates sufficient size to store the entire string literal, including the null character.

+3
source

You use the second, because in the second, if you want to initialize another string that has more than 3 characters, so it will take care automatically.

Code example

 int main() { int i; char arr[] = "xyz Hello World"; for(i=0;i<sizeof(arr)-1;i++){ printf("%c",arr[i]); } printf("\n"); return 0; } 

if you use the first one, so when you want to save more than 3 char lines, it will show warning at compile time

Attention

  warning: initializer-string for array of chars is too long [enabled by default] char arr[3] = "xyz Hello World"; 

so you should use the second, it is best to initialize the character array with string .

+2
source

Consider also the use of

 const char* arr = "xyz"; 

This is the same (except for the 'const' keyword, which makes it so that you do not accidentally modify the array), but the data will not be copied onto the stack, you are using a static copy in the executable data segment. This is especially important for large strings.

0
source

Source: https://habr.com/ru/post/1263829/


All Articles