【问题标题】:Error on Multiple Array Dynamic Memory Allocation多阵列动态内存分配错误
【发布时间】:2019-01-20 12:47:11
【问题描述】:

请告知为什么以下不起作用。使用VC2017:

long **l;
l = new long [5][7];

它显示错误为:

“类型为“long*[7]”的值不能分配给一个实体 长**"...

我该如何解决?

【问题讨论】:

  • long **l=NULL; l=新长 [5][7];

标签: c++ arrays pointers visual-c++


【解决方案1】:

您需要声明并初始化指向long* 的第一个指针数组,然后将其分配给每个自己的数组:

long** l = new long*[5]; // declare array of pointer of 5 cell
for(int i = 0; i < 5; ++i)
    l[i] = new long[7]; // assign to each cell array with 7 cells

请记住,使用 new 分配的任何内容都是在堆上创建的,并且必须使用 delete 取消分配。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-19
    • 1970-01-01
    • 2016-01-25
    • 2020-01-14
    • 2011-02-26
    相关资源
    最近更新 更多