Two string literals have the same pointer value?

后端 未结 5 1683
慢半拍i
慢半拍i 2020-11-27 08:14

When I run this program using MinGW, im getting output as \"=\"

#include

using namespace std;

int main()
{
 char *str1 = \"Hello\";
 char *         


        
5条回答
  •  野性不改
    2020-11-27 09:01

    There are no guarantees that the two pointers are pointing to different memory locations. Maybe it is because optimizations, or the compiler uses its own rules... the behavior is "Implementation Defined".

    According to the standard (C++11 §2.14.5 String Literals):

    Whether all string literals are distinct (that is, are stored in nonoverlapping objects) is implementation defined.

提交回复
热议问题