MrRarri
MrRarri

Reputation: 55

Pass a double** in a function that takes double as input in c

I tried to pass the double** as a double like this :

double calculErreurLocale(double erreurGlobale, double entree)
{
    return entree*(1-entree)*erreurGlobale;
}

double** matrice1()
{
    double** matrice_I;
    int i;

    matrice_I=malloc(sizeof(double*)*ligne);
    for (i=0; i<ligne; i++)
    {
        matrice_I[i]=malloc(sizeof(double)*colonne);
    }

    //affiche_matriceFloat(matrice_I);
    return matrice_I;
}

int main()
{
    double **matrice = matrice1();
    matrice[0][0]=-1;
    matrice[1][0]=-1;

    calculErreurLocale(errGlobale, matrice);
 return = 0;
}

Obviously I get an error. I tried to cast it or something like this :

calculErreurLocale(errGlobale, (double) matrice);

Then I get "error: pointer cannot be cast to type 'double'".

Is my problem something else, maybe the design is not correct...

Upvotes: 0

Views: 107

Answers (2)

phg
phg

Reputation: 315

matrice is an array of pointers whereas your function expects a double. You can only pass it one element of the matrix at a time. For the first item in the first row:

calculErreurLocale(errGlobale, matrice[0][0]);

If you want to apply the function to each element instead, do it in a loop similar to what matrice1() does.

Upvotes: 0

damagedCoda
damagedCoda

Reputation: 73

As per your function definition for calculErreurLocale its expected that you pass an element of the 2-D array matrice. If you, however, want to pass the entire array into the function and then manipulate the same, I would suggest you change the function definition to double calculErreurLocale(double erreurGlobale, double** entree). The code snippet is too ambiguous to understand what you want to do with the code, but explicitly typecasting your array to a double value is not the answer.

Upvotes: 1

Related Questions