Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The brace in C language char array initialization

Tags:

c

string

char

Now I have encounter a problem as below:

int foo(void){
    char a[10] = "foo";
    char b[10] = {"foo"};
    ...
}

I used gdb to inspect the content of both 'a' and 'b' and they looked exactly the same.

Is there any difference between the variabel a and b?

like image 654
fabregaszy Avatar asked Mar 04 '14 02:03

fabregaszy


1 Answers

They are the same:

C11 §6.7.9 Initialization

An array of character type may be initialized by a character string literal or UTF−8 string literal, optionally enclosed in braces. Successive bytes of the 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.

like image 162
Yu Hao Avatar answered Sep 29 '22 07:09

Yu Hao