Error in Passing 2d Array in C/C++

前端 未结 1 1912
清酒与你
清酒与你 2020-12-22 00:37

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 in

相关标签:
1条回答
  • 2020-12-22 01:02

    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;
    }
    
    0 讨论(0)
提交回复
热议问题