Braces around string literal in char array declaration valid? (e.g. char s[] = {“Hello World”})

前端 未结 6 544
我在风中等你
我在风中等你 2020-12-03 06:32

By accident I found that the line char s[] = {\"Hello World\"}; is properly compiled and seems to be treated the same as char s[] = \"Hello World\";

6条回答
  •  北海茫月
    2020-12-03 07:05

    [...] In fact if I change it to char *s[] = {"Hello World"}; the compiler accepts it as well, as expected

    The compiler accepets it,because actually, you're making an array 2D of undefined size elements,where you stored one element only,the "Hello World" string. Something like this:

    char* s[] = {"Hello world", "foo", "baa" ...};
    

    You can't omit the bracets in this case.

提交回复
热议问题