What is the type of string literals in C and C++?

前端 未结 4 1742
感动是毒
感动是毒 2020-11-21 22:47

What is the type of string literal in C? Is it char * or const char * or const char * const?

What about C++?

4条回答
  •  无人及你
    2020-11-21 23:40

    A C string literal has type char [n] where n equals number of characters + 1 to account for the implicit zero at the end of the string.

    The array will be statically allocated; it is not const, but modifying it is undefined behaviour.

    If it had pointer type char * or incomplete type char [], sizeof could not work as expected.

    Making string literals const is a C++ idiom and not part of any C standard.

提交回复
热议问题