samedi 4 novembre 2023

My VScode cannot initialize strings created dynamically using 'new'.What is the wrong?

my code:

#include<iostream>
using namespace std;


int main()
{
    char * ar1 = new char[3] {'a','b','\0'};
    char * ar2 = new char[3] {"ab"};
    const char * ar3 =new char[3]{"ab"};
    cout << ar1 << endl;
    cout << ar2;
    cin.get();
    return 0;
}

Ar1 can be initialized successfully, but ar2 and ar3 cannot. What is the error?

At first, I thought my compiler didn't support C++11, but I found that it could initialize ar1, indicating that it was supported, so I'm confused now.

Aucun commentaire:

Enregistrer un commentaire