错误在C / C传递二维数组++ [英] Error in Passing 2d Array in C/C++
本文介绍了错误在C / C传递二维数组++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我收到错误:
无法转换INT **为int * [5] ...
在我的code。我知道如何通过2 D阵列功能,但是我在这里卡住了。
在code是用于在一个邻接矩阵深度优先遍历。
的#include<&iostream的GT;
#包括LT&; cstdlib>
#包括LT&; CString的GT;
#包括LT&;&cstdio GT;使用命名空间std;的#define米5
#定义N 5无效DFS(INT政[] [N],INT I,诠释访问[])
{
访问了由[i] = 1;
诠释J;
为(J = 0; J< N; J ++)
{
如果(访问[J]放大器;!&安培;政[i] [j]的== 1)
DFS(G,J,访问);
}
}诠释的main()
{INT R,C,I;
的printf(请输入尺寸\\ n);
scanf函数(%D,&安培; R和C);
R =米; C = N;
INT ** G =(INT **)的malloc(R *的sizeof(INT *));
对于(i = 0; I< R;我++)
{
政[i] =(INT *)malloc的(C *的sizeof(INT));
}
的printf(请输入矩阵\\ n);
诠释J; 对于(i = 0; I< R;我++)
为(J = 0; J&c为C; J ++)
scanf函数(%D,G [I] [J]);
为int *参观=(INT *)malloc的(R *的sizeof(INT));
memset的(走访,0,sizeof的(访问)); DFS(G,0,访问);}
解决方案
我想你需要的是这个,它是交流的解决方案,如果你想要C ++,你应该使用的std ::矢量
代替。
的#include<&stdlib.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&stdio.h中GT;使用命名空间std;
无效DFS(INT ** G,INT I,为int *参观,INT C)
{
诠释J; 访问了由[i] = 1;
为(J = 0; J&c为C; J ++)
{
如果((参观[J] == 0)及和放大器;(G [i] [j]的== 1))
DFS(G,J,走访,C);
}
}诠释的main()
{
INT R,C,I,J;
INT ** G组;
INT *参观; 的printf(请输入尺寸\\ n);
scanf函数(%D,&安培; R和C); G =的malloc(R *的sizeof(* G));
如果(G == NULL)
返回-1;
走访=的malloc(R *的sizeof(*参观));
如果(访问== NULL)
{
免费(G);
返回-1;
}
memset的(走访,0,R * sizeof的(*参观)); 的printf(请输入矩阵\\ n);
对于(i = 0; I< R;我++)
{
政[I] =的malloc(C *的sizeof(** G));
如果(G [I] == NULL)
{
为(J = - 1; J> = 0; --j)
免费(G [J]);
免费(G); 返回-1;
}
为(J = 0; J&c为C; J ++)
scanf函数(%d个,&安培;(G [I] [J]));
}
DFS(G,0,走访,C); /* 现在免费 */
免费(访问);
对于(i = 0; I< R;我++)
免费(G [I]);
免费(G); 返回0;
}
这也可能是一个C ++的解决方案
的#include<&iostream的GT;
#包括LT&; cstdlib>
#包括LT&; CString的GT;
#包括LT&;&cstdio GT;无效DFS(INT ** G,INT I,为int *参观,INT C)
{
诠释J; 访问了由[i] = 1;
为(J = 0; J&c为C; J ++)
{
如果((参观[J] == 0)及和放大器;(G [i] [j]的== 1))
DFS(G,J,走访,C);
}
}诠释的main()
{
INT R,C,I,J;
INT ** G组;
INT *参观; 的printf(请输入尺寸\\ n);
scanf函数(%D,&安培; R和C); G =新为int * [R]。
走访= INT新[R]。 memset的(走访,0,R * sizeof的(*参观)); 的printf(请输入矩阵\\ n);
对于(i = 0; I< R;我++)
{
政[i] = INT新[C];
为(J = 0; J&c为C; J ++)
scanf函数(%d个,&安培;(G [I] [J]));
}
DFS(G,0,走访,C); /* 现在免费 */
删除[]访问;
对于(i = 0; I< R;我++)
删除[]政[我]
删除[] G组; 返回0;
}
和最后用的std ::矢量
的#include<&iostream的GT;
#包括LT&; cstdlib>
#包括LT&; CString的GT;
#包括LT&;&cstdio GT;
#包括LT&;矢量>使用命名空间std;
无效DFS(常量矢量<矢量<&INT GT;>&放大器; G,INT I,矢量<&INT GT;&安培;走访)
{
为size_t焦耳; 访问了由[i] = 1;
为(J = 0; J< visited.size(); J ++)
{
如果((参观[J] == 0)及和放大器;(G [i] [j]的== 1))
DFS(G,J,访问);
}
}诠释的main()
{
INT R,C,I,J;
的printf(请输入尺寸\\ n);
scanf函数(%D,&安培; R和C); 矢量<矢量<诠释> > G(R,矢量< INT>(c)项);
矢量<诠释>被访问的(R,0); 的printf(请输入矩阵\\ n);
对于(i = 0; I< R;我++)
{
为(J = 0; J&c为C; J ++)
scanf函数(%d个,&安培;(G [I] [J]));
}
DFS(G,0,访问); 返回0;
}
I am getting the error:
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\n");
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\n");
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);
}
解决方案
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\n");
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\n");
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;
}
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\n");
scanf("%d %d", &r, &c);
G = new int*[r];
visited = new int[r];
memset(visited, 0, r * sizeof(*visited));
printf("Enter the Matrix\n");
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;
}
And finally with 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\n");
scanf("%d %d", &r, &c);
vector< vector<int> > G(r, vector<int>(c));
vector<int> visited(r, 0);
printf("Enter the Matrix\n");
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传递二维数组++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文