计算机小混子 2022-03-12 19:22 采纳率: 100%
浏览 22
已结题

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


#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

  • 写回答

4条回答 默认 最新

  • [PE]经典八炮 2022-03-12 19:47
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 3月20日
  • 已采纳回答 3月12日
  • 创建了问题 3月12日

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大