Home > other >  How to pass a 2D arrays of pointers to 3D arrays of pointers (function and class) and return 3d arra
How to pass a 2D arrays of pointers to 3D arrays of pointers (function and class) and return 3d arra

Time:11-21

I am currently working on C , creating a program of the analysis of matrices. As far as i saw those can be created using an arrays of arrays like this array2D[3][3]={{1,2,3},{4,5,6},{7,8,9}}.

Therefore, what i did was to generate a function in a class, such a function must return a 2D array. Then i created another class in order to generate the other array of array of arrays, but this 3D arrays need the data obtained by the previous class, remember the previous class holds the values of the matrix in a variable called int **degreesOfFreedom. Here is where the problem arises this second class needs the values of the double pointer and the problem like this appears.

error: cannot convert ‘int***’ to ‘int**’ in assignment

As far as i can see the error comes when trying to pass the 2D pointers arrays into the 3D pointers function.

By the way i already checked several ways and i saw that one of them is replacing the double pointer ** in the declaration of the variable inside of the function, and replace it by [][]. I already tried that, it didn't solve the problem, also i would not like to do that way because in the future i will have matrices of millions per millions elements.

If someone can help me or address me through the right will be nice

Thanks in advance

This is my code

#include <iostream>
#include <string>
#include <fstream>

class MatrixOfDegreesOfFreedom
{
public:
    int X, Y;
    int M = 0;
public:
    int **matrixOfDegreesOfFreedom(int rows, int cols)
    {
        X = rows;
        Y = cols;
        int** matrix = new int*[X];
        for (int i = 0; i < X;   i)
        {

            matrix[i] = new int[Y];
            for (int j = 0; j < Y;   j)
            {
                matrix[i][j] = M;
                M = M   1;
            }
        }
        return matrix;
    }

    //constructor
    MatrixOfDegreesOfFreedom()
    {
        
    }

    //destructor
    ~MatrixOfDegreesOfFreedom()
    {

    }
};

class MatrixOfIndexes
{
public:
    int X, Y, Z;
    int M = 0;
public:
    int ***matrixOfIndexes(int rows, int cols, int colsTwo, int conect[][2], int **DoF)
    {
        X = rows;
        Y = cols;
        Z = colsTwo;
        int*** matrix = new int**[X];
        for (int i = 0; i < X;   i)
        {
            M = 0;
            matrix[i] = new int*[Y];
            for (int j = 0; j < Y;   j)
            {
                matrix[i][j] = new int [Z];
                for (int t = 0; t < Z;   t)
                {
                    matrix[i][j][t] = DoF[conect[i][j]][t];
                }
                
                M = M   1;
            }
        }
        return matrix;
    }

    //constructor
    MatrixOfIndexes()
    {
        
    }

    //destructor
    ~MatrixOfIndexes()
    {

    }
};

int main(int argc, char const *argv[])
{
    #ifndef OUTPUT
        freopen("output.txt", "w", stdout); // file to store the output data.
    #endif

    int numberOfNodes = 3; // number of nodes
    int numberOfDegreesOfFreedomPerNode = 2; //Number of Degrees of Freedom per node
    int **degreesOfFreedom = {}; //number of degree of freedom
    int numberOfDegreesOfFreedomPerElement = 4; //Number of Degrees of Freedom per element

    int numberOfElements = 3;
    int connectivity[numberOfElements][2] = {{0,1},{2,1},{0,2}}; // Conectivity matrix along with the property
    
    int **indexes = {};


    MatrixOfDegreesOfFreedom tableOfDegreesOfFreedom;
    degreesOfFreedom = tableOfDegreesOfFreedom.matrixOfDegreesOfFreedom(numberOfNodes, numberOfDegreesOfFreedomPerNode);
    MatrixOfIndexes tableOfIndexes;
    indexes = tableOfIndexes.matrixOfIndexes(numberOfElements, numberOfDegreesOfFreedomPerElement, numberOfDegreesOfFreedomPerNode, connectivity, degreesOfFreedom);


    std::cout<< "finishing" << std::endl;

    return 0;
}

CodePudding user response:

The problem is that the function matrixOfIndexes returns a 3-dimensional pointer (int***) but the array you're assigning that return value to is a two-dimensional one (int**). The types have to match.

To fix it just add an extra * to the declaration of indexes:

int*** indexes = {};
  • Related