【发布时间】:2021-03-17 19:14:23
【问题描述】:
函数中的输入数组有问题; 在这段代码中,我从用户那里获取一个带有索引的数组参数,该函数将打印带有参数保留的二维数组表; 这是代码:
#include <iostream>
#include <windows.h>
using namespace std ;
const char * table[3][2]={{"m1","m2"},{"n1","n2"},{"h1","h2"}} ;
void setTable(char *array,int n,int m) {
for(int i=0 ; i<n;i++){
for(int j=0 ; j<m;j++) {
cout<<array[i][j]<<"---" ; //print array
}
}
}
int main(){
setTable((char * )table,4,2) ; // send array with indexes to function
return 0;
}
但是当我运行它时出现错误:
In function 'void setTable(char*, int, int)':
[Error] invalid types 'char[int]' for array subscript
【问题讨论】:
-
你使用原始数组而不是容器类有什么原因吗?
标签: c++ arrays function multidimensional-array