编译器报错如下图,求解释


#include <iostream>
using namespace std;
int main()
{
  //定义一个包含指针成员的结构类型
    struct test
    {
        char* str;
        int* ip;
    }x;
    //使用结构变量x中的整型指针
    x.ip = new int[1];//分配1个单元
    * (x.ip) = 100;
    cout << "x.ip:" << x.ip << '\t' << *(x.ip) << endl;
    cout << "____________________________________________" << endl;
    delete x.ip;
    x.ip = new int[5];//分配5个单元
    for (int i = 0; i < 5; i++)
        *(x.ip + i) = 100 + i;
    cout << "x.ip:" << endl;
    for (int i = 0; i < 5; i++)
        cout << x.ip + i << '\t' << (*(x.ip + i)) << endl;
    delete x.ip;
    cout << "_____________________________________________" << endl;
    //使用结构变量x中的字符型指针str
    x.str = new char('A');//分配1个单元
    cout << "x.str:" << *(x.str) << endl;
    cout << "_________________________________________________" << endl;
    delete x.str;
    x.str = new char[5];//分配多个单元
    *(x.str) = 'G';
    *(x.str + 1) = 'o';
    *(x.str + 2) = 'o';
    *(x.str + 3) = 'd';
    *(x.str + 4) = '\0';
    cout << "x.str:" << x.str << endl;
    delete x.str;
    cout << "_______________________________________________________" << endl;
    //在声明结构变量时初始化
    test y = { "Very Good!",NULL };
    cout << "y.str:" << y.str << endl;
    cout << "y.ip:" << y.ip << endl;







}

img

我估计是const char*不能随便赋值给char*
C++是强类型语言,对于类型要求比较严格

指针没法直接用字符串赋值吧,你可以先把字符串给一个字符数组,然后把字符数组的地址给指针

[Warning] ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]、
字符串常量不能那样直接转换成字符指针类型

希望对题主有所帮助,可以的话,帮忙点个采纳!

错误提示是什麽啊?