Home > other >  error:invalide operands to binary (have 'float' and 'float **')
error:invalide operands to binary (have 'float' and 'float **')

Time:05-17

    void clc_moy_mdl(int n, int m,float *mt[3][30])
{
    float s;
    int i,j;
    s=0;

    remplire_matrice(n,m,tab,mt);
    for(j=1;i<=n;j  )
    {
        for(i=1;i<=(m-1);i  )
        {
            s =s  mt[i][j];
            printf("%f",s);
        }
        mt[i][j]= s /(m);
    }
}

this is a procedure from a program that I write it I have an error in line 12 error:invalid operands to binary (have 'float' and 'float **') How can I solve it?

CodePudding user response:

The parameter mt is declared as having the array type float *[3][30]

void clc_moy_mdl(int n, int m,float *mt[3][30])

Thus the expression mt[i][j] has the type float * and in these statements

s =s  mt[i][j];

and

mt[i][j]= s /(m);

there are used incorrectly operations with a pointer and a float.

Either instead you have to write

s =s   *mt[i][j];

and

*mt[i][j]= s /(m);

or to declare the function like

void clc_moy_mdl(int n, int m,float mt[3][30])

Pay attention to that array indices start from 0. So it seems the for loops like this

for(j=1;i<=n;j  )

in your program can produce undefined behavior. You should write

for(j=0;i<n;j  )

CodePudding user response:

float *mt[3][30] defines mt as 2D array of pointers to float.

Remove the *

You can also use the passed sizes to declare this parameter.


void clc_moy_mdl(size_t n, size_t m,float mt[n][m])
{
    float s = 0.0f;
    size_t i,j;

    for(j = 0; i < n; j  )
    {
        for(i = 0; i < m; i  )
        {
            s  = mt[i][j];
            printf("%f", s);
        }
        mt[i][j]= s / m;
    }
}

Remember that indexes start from 0 and use the correct types for sizes (size_t)

  • Related