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

前端 未结 6 545
我在风中等你
我在风中等你 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:14

    This is allowed by the C++ standard as well, Citation:

    [dcl.init.string] §1

    An array of narrow character type ([basic.fundamental]), char16_t array, char32_t array, or wchar_t array can be initialized by a narrow string literal, char16_t string literal, char32_t string literal, or wide string literal, respectively, or by an appropriately-typed string literal enclosed in braces ([lex.string]). [snip]

提交回复
热议问题