vendredi 27 décembre 2019

How to manipulate an array of structure containing 2D array

Using C++, I am implementing an array of structure HiddenLayer defined as

    struct HiddenLayer
    {
        int prev;     ///Number of Rows in node
        int next;     ///Number of Columns in node

        float **node; ///2D array pointer
    };

The array of structure is initialized in the main routine and node is the pointer to the 2D array inside the structure. I am initializing this array as

    int main()
    {
        struct HiddenLayer HLayer[1]; 

        HLayer[0].prev = 1;  //Num of rows
        HLayer[0].next = 3;  //num of col

        HLayer[0].node = (float *) malloc((HLayer[0].prev) * sizeof(float *));  

        for(int i=0;i<HLayer[0].prev;i++)
            HLayer[0].node[i] = malloc(HLayer[0].next * sizeof(float));


        return 0;
    }

But I get this error:

In function ‘int main()’:
main.cpp:22:73: error: cannot convert ‘float*’ to ‘float**’ in assignment
     HLayer[0].node = (float *) malloc((HLayer[0].prev) * sizeof(float *));  
                                                                         ^

>main.cpp:25:35: error: invalid conversion from ‘void*’ to ‘float*’ [-fpermissive]
         HLayer[0].node[i] = malloc(HLayer[0].next * sizeof(float));

I followed the answers given Here and Here

What wrong am I doing?

Aucun commentaire:

Enregistrer un commentaire