C char array initialization

I'm not sure what will be in the char array after initialization in the following ways.

1. char buf[10] = "";
2. char buf[10] = " ";
3. char buf[10] = "a";

For case 2, I think buf[0] should be ' ' , buf[1] should be '\0' , and from buf[2] to buf[9] will be random. For case 3, I think buf[0] should be 'a' , buf[1] should be '\ 0', and from buf[2] to buf[9] will be random.

Is it correct?

And for case 1, what will happen in buf ? buf[0] == '\0' and from buf[1] to buf[9] will be random content?

+67
c arrays initialization char buffer
Sep 08
source share
6 answers

This is not how you initialize the array, but for:

  • First declaration:

     char buf[10] = ""; 

    equivalently

     char buf[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 
  • Second announcement:

     char buf[10] = " "; 

    equivalently

     char buf[10] = {' ', 0, 0, 0, 0, 0, 0, 0, 0, 0}; 
  • Third declaration:

     char buf[10] = "a"; 

    equivalently

     char buf[10] = {'a', 0, 0, 0, 0, 0, 0, 0, 0, 0}; 

As you can see, no random content: if there are fewer initializers, the remaining array is initialized with 0 . This case, even if the array is declared inside the function.

+138
Sep 08 '13 at
source share

Edit: The OP (or editor) silently changed some single quotes in the original question to double quotes at some point after providing this answer.

As a result, your code will lead to compiler errors. Your first piece of code:

 char buf[10] ; buf = '' 

is twice illegal. Firstly, in C there is no such thing as an empty char . You can use double quotes to indicate an empty string, for example:

 char* buf = ""; 

This will give you a pointer to a NUL string, i.e. a single character string containing only the NUL . But you cannot use single quotes without anything inside them - it is undefined. If you need to assign a NUL , you must specify it:

 char buf = '\0'; 

A backslash is needed to disambiguate the character '0' .

 char buf = 0; 

does the same thing, but the first, in my opinion, is a bit less ambiguous to read.

Secondly, you cannot initialize arrays after they are defined.

 char buf[10]; 

declares and defines an array. The buf array id is now an address in memory, and you cannot change where buf indicates the destination. So,

 buf = // anything on RHS 

is illegal. For this reason, your second and third code snippets are illegal.

To initialize an array, you must do this during the definition:

 char buf [10] = ' '; 

will provide you with a 10-digit array with the first char being the space '\040' , and the rest will be NUL , i.e. '\0' . When an array is declared and determined using the initializer, the elements of the array (if any) past those with the given initial values ​​are automatically supplemented with 0 . There will be no “random content”.

If you declare and define an array, but do not initialize it, as in the following:

 char buf [10]; 

You will have random content in all elements.

+17
Sep 08 '13 at 22:20
source share
  • It is equivalent

     char buf[10] = ""; char buf[10] = {0}; char buf[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 
  • It is equivalent

     char buf[10] = " "; char buf[10] = {' '}; char buf[10] = {' ', 0, 0, 0, 0, 0, 0, 0, 0, 0}; 
  • It is equivalent

     char buf[10] = "a"; char buf[10] = {'a'}; char buf[10] = {'a', 0, 0, 0, 0, 0, 0, 0, 0, 0}; 
+12
Sep 29 '14 at 16:15
source share

The relevant part of the initialization of the standard project C11 standard n1570 6.7.9 reads:

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

and

21 If there are fewer initializers in the list with braces than there are elements or members of the population or fewer characters in the string literal used to initialize an array of known sizes than there are elements in the array, the rest of the population must be initialized implicitly the same as the objects having a static storage duration.

Thus, '\ 0' is added , if there is enough space , and the remaining characters are initialized with the value that will be initialized static char c; inside the function.

Finally,

10 If an object with automatic storage duration is not explicitly initialized, its value is undefined. If an object with a static or thread storage duration is not explicitly initialized, then:

[-]

  • if it has an arithmetic type, it is initialized to zero (positive or unsigned);

[-]

Thus, char is an arithmetic type, the rest of the array can also be initialized to zeros.

+6
Jul 05 '16 at 11:03
source share

Interestingly, at any time in the program, you can initialize arrays if they are members of a struct or union .

Program Example:

 #include <stdio.h> struct ccont { char array[32]; }; struct icont { int array[32]; }; int main() { int cnt; char carray[32] = { 'A', 66, 6*11+1 }; // 'A', 'B', 'C', '\0', '\0', ... int iarray[32] = { 67, 42, 25 }; struct ccont cc = { 0 }; struct icont ic = { 0 }; /* these don't work carray = { [0]=1 }; // expected expression before '{' token carray = { [0 ... 31]=1 }; // (likewise) carray = (char[32]){ [0]=3 }; // incompatible types when assigning to type 'char[32]' from type 'char *' iarray = (int[32]){ 1 }; // (likewise, but s/char/int/g) */ // but these perfectly work... cc = (struct ccont){ .array='a' }; // 'a', '\0', '\0', '\0', ... // the following is a gcc extension, cc = (struct ccont){ .array={ [0 ... 2]='a' } }; // 'a', 'a', 'a', '\0', '\0', ... ic = (struct icont){ .array={ 42,67 } }; // 42, 67, 0, 0, 0, ... // index ranges can overlap, the latter override the former // (no compiler warning with -Wall -Wextra) ic = (struct icont){ .array={ [0 ... 1]=42, [1 ... 2]=67 } }; // 42, 67, 67, 0, 0, ... for (cnt=0; cnt<5; cnt++) printf("%2d %c %2d %c\n",iarray[cnt], carray[cnt],ic.array[cnt],cc.array[cnt]); return 0; } 
+3
May 13 '14 at 10:51
source share

I'm not sure, but I usually initialize the array to "" in this case, I don't need to worry about the null end of the string.

 main() { void something(char[]); char s[100] = ""; something(s); printf("%s", s); } void something(char s[]) { // ... do something, pass the output to s // no need to add s[i] = '\0'; because all unused slot is already set to '\0' } 
0
Apr 01 '16 at 1:41 on
source share



All Articles