Error in Passing 2d Array in C/C++(在 C/C++ 中传递二维数组时出错)
问题描述
我收到错误:
无法将 int** 转换为 int*[5]...
Cannot convert int** to int*[5]...
在我的代码中.我知道如何将二维数组传递给函数,但我被困在这里.
in my code. I know how to pass 2 D arrays to functions but I am stuck here.
该代码用于对邻接矩阵进行深度优先遍历.
The code is intended for Depth first Traversal over a Adjacency Matrix.
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
using namespace std;
#define m 5
#define n 5
void dfs(int G[][n],int i,int visited[])
{
visited[i]=1;
int j;
for(j=0;j<n;j++)
{
if(!visited[j]&&G[i][j]==1)
dfs(G,j,visited);
}
}
int main()
{ int r,c,i;
printf("Enter Dimensions
");
scanf("%d %d",&r,&c);
r = m; c = n;
int **G = (int **)malloc(r*sizeof(int*));
for(i=0;i<r;i++)
{
G[i] = (int *)malloc(c*sizeof(int));
}
printf("Enter the Matrix
");
int j;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",G[i][j]);
int *visited = (int*)malloc(r*sizeof(int));
memset(visited,0,sizeof(visited));
dfs(G,0,visited);
}
推荐答案
我认为你需要的是这个,它是 ac 解决方案,如果你想要 c++ 你应该使用 std::vector
代替.
I think what you need is this, it is a c solution, if you want c++ you should probably use std::vector
instead.
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
using namespace std;
void dfs(int **G, int i, int *visited, int c)
{
int j;
visited[i] = 1;
for(j = 0 ; j < c ; j++)
{
if((visited[j] == 0) && (G[i][j] == 1))
dfs(G, j, visited, c);
}
}
int main()
{
int r, c, i, j;
int **G;
int *visited;
printf("Enter Dimensions
");
scanf("%d %d", &r, &c);
G = malloc(r * sizeof(*G));
if (G == NULL)
return -1;
visited = malloc(r * sizeof(*visited));
if (visited == NULL)
{
free(G);
return -1;
}
memset(visited, 0, r * sizeof(*visited));
printf("Enter the Matrix
");
for(i = 0 ; i < r ; i++)
{
G[i] = malloc(c * sizeof(**G));
if (G[i] == NULL)
{
for (j = i - 1 ; j >= 0 ; --j)
free(G[j]);
free(G);
return -1;
}
for(j = 0 ; j < c ; j++)
scanf("%d", &(G[i][j]));
}
dfs(G, 0, visited, c);
/* now free */
free(visited);
for(i = 0 ; i < r ; i++)
free(G[i]);
free(G);
return 0;
}
这也可能是一个 C++ 解决方案
this could also be a c++ solution
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
void dfs(int **G, int i, int *visited, int c)
{
int j;
visited[i] = 1;
for(j = 0 ; j < c ; j++)
{
if((visited[j] == 0) && (G[i][j] == 1))
dfs(G, j, visited, c);
}
}
int main()
{
int r, c, i, j;
int **G;
int *visited;
printf("Enter Dimensions
");
scanf("%d %d", &r, &c);
G = new int*[r];
visited = new int[r];
memset(visited, 0, r * sizeof(*visited));
printf("Enter the Matrix
");
for(i = 0 ; i < r ; i++)
{
G[i] = new int[c];
for(j = 0 ; j < c ; j++)
scanf("%d", &(G[i][j]));
}
dfs(G, 0, visited, c);
/* now free */
delete[] visited;
for(i = 0 ; i < r ; i++)
delete[] G[i];
delete[] G;
return 0;
}
最后是 std::vector
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
using namespace std;
void dfs(const vector< vector<int> > &G, int i, vector<int> &visited)
{
size_t j;
visited[i] = 1;
for(j = 0 ; j < visited.size() ; j++)
{
if((visited[j] == 0) && (G[i][j] == 1))
dfs(G, j, visited);
}
}
int main()
{
int r, c, i, j;
printf("Enter Dimensions
");
scanf("%d %d", &r, &c);
vector< vector<int> > G(r, vector<int>(c));
vector<int> visited(r, 0);
printf("Enter the Matrix
");
for(i = 0 ; i < r ; i++)
{
for(j = 0 ; j < c ; j++)
scanf("%d", &(G[i][j]));
}
dfs(G, 0, visited);
return 0;
}
这篇关于在 C/C++ 中传递二维数组时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 C/C++ 中传递二维数组时出错


- 如何对自定义类的向量使用std::find()? 2022-11-07
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- STL 中有 dereference_iterator 吗? 2022-01-01
- 静态初始化顺序失败 2022-01-01
- C++ 协变模板 2021-01-01
- 近似搜索的工作原理 2021-01-01
- 从python回调到c++的选项 2022-11-16
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- Stroustrup 的 Simple_window.h 2022-01-01