Nir*_*van -1 c++ arrays new-operator multidimensional-array dynamic-arrays
我在这个程序的两个不同的地方得到了相同的错误,它应该是一个1d,一个2d和一个3d数组并存储值并同时显示它们.错误:下标需要数组或指针类型/表达式必须具有指针到对象类型,错误是表达式c [r] [c] [depth]
#include<iostream>
using namespace std;
#define ROW 5
#define COL 5
#define DEPTH 5
int main()
{
int *a; // 1d array
a=new int [COL];
int (*b) [COL]; //2d array
b=new int [ROW][COL];
int (*c)[ROW][COL];
c=new int [ROW][COL][DEPTH]; // 3d array
//---------------------------------------------------------------------------------
// storing values in the arrays:
for(int i=0;i<COL;i++)
{
a[i]=i+2;
cout << a[i];
}
// 2d array
for(int r=0;r<ROW;r++)
{
for(int c=0;c<COL;c++)
{
b[r][c]=r+c+2;
cout << b[r][c];
}
}
// 3d array
for(int r=0;r<ROW;r++)
{
for(int c=0;c<COL;c++)
{
for(int depth=0;depth<DEPTH;depth++)
{
c[r][c][depth]=r+c+depth+2; //error
cout << c[r][c][depth]; //same error
}
}
}
//-------------------------------------------------------------------------------------
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
287 次 |
| 最近记录: |