【发布时间】:2021-05-27 16:56:13
【问题描述】:
我正在尝试将二维字符数组传递给函数,但是 vs 代码给了我以下错误消息:
无法将 'char ()[3]' 转换为 'char ()[10]'gcc
代码如下:
#include<string>
using namespace std;
void NodeDetect(char grid[][3], int height, int width)
{
cout << "\nThe grid output:\n";
for(int i = 0; i < height; ++i)
{
for(int j = 0; j < width; ++j)
if(grid[i][j] == '0')
{
cout << '\n' << i << '\t' << j << ", ";
if(grid[i][j + 1] == '0' && (j + 1) < width)//right neighbour
cout << i << '\t' << (j + 1) << ", ";
else if(grid[i][j + 1] == '.' || (j + 1) == width)
cout << "-1 -1, ";
if(grid[i + 1][j] == '0' && (i + 1) < height)//bottom neighbour
cout << (i + 1) << '\t' << j << ", ";
else if(grid[i + 1][j] == '.' || (i + 1) == height)
cout << "-1 -1";
}
cout << '\n';
}
}
int main()
{
string line;
char grid[3][3];
int height, width; //height = rows
cout << "Enter the height and the width:\t";//width = columns
cin >> height >> width;
cout << "\nEnter the strings:\n";
for(int i = 0; i < height; ++i)//initializing the grid
cin >> grid[i];
/*
cout << "\nThe grid:\n";
for(int i = 0; i < height; ++i) //displaying the grid
{
for(int j = 0; j < width; ++j)
cout << grid[i][j] << '\t';
cout << '\n';
}
*/
NodeDetect(grid, height, width);
return 0;
}
我正在尝试将二维数组 grid 传递给函数 NodeDetect
【问题讨论】:
-
这 10 个是从哪里来的?
-
添加
#include <iostream>后我无法重现您的编译器错误:ideone.com/5TSDAI -
@kushagra kartik 在呈现的代码中没有包含 10 个元素的字符数组。提供相关代码。
-
为什么会有变量:
height和width,您在评论中描述为rows和columns
标签: c++ arrays c++11 visual-studio-code computer-science