char four[4] = “four”; What are the correct semantics for this statement?

后端 未结 4 2023
逝去的感伤
逝去的感伤 2021-01-18 07:23
int main(void)
{
    char four[4] = \"four\";
    return 0;
}

When compiled as a C++ program, G++ reports

xxx.cpp: In function int

4条回答
  •  执笔经年
    2021-01-18 08:01

    What you're seeing is a difference between C and C++. C allows you to have extra initializers, which are ignored. C++ prohibits this -- if you specify a size for a string (or array) it must be large enough to accommodate all the initializers (including the NUL terminator, in the case of a string), or the code is ill-formed (standardese for "it's not allowed -- expect the compiler to reject it").

提交回复
热议问题