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++ 中传递二维数组时出错
基础教程推荐
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07