invalid type argument of unary '*' (have 'double') in C

Asked

Viewed 267 times

-1

I do not understand why this problem is happening, I have already done it in other programs and never had problem, below this function q is giving error, if necessary put all the code.

    double Perimetro(Poligono P){
        Poligono A, B; 
        int i = 0;
        double *D; //armazena a distancia entre dois pontos
        double numvert, soma = 0;
        numvert = NumeroDeVertices(P);
        D = (double*)malloc(sizeof((double)*numvert));
        A = P;
        B = P->prox;
        for (i = 0; i < numvert; i++){
            D[i] = Distancia(A, B);
            A->prox = B;
            B = B->prox;
            soma = soma + D[i];
        }
        return soma;
    }
  • I believe here: D = (double*)malloc(sizeof((double)numvert); should be: D = (double) malloc(sizeof(double) * numvert);

  • Why are you declaring the number of vertices as a double? Does it make sense to have 3.75 vertices? And why do you create the distance vector, when your problem is clear that it is not necessary to store this information? And what you hoped to get the size of (double)*numvert? Or you wanted to get the size of double then multiply by numvert?

1 answer

0


You are multiplying a double inside the sizeof. the correct is: size of the data type * how many spaces in memory you want.

malloc(sizeof(double) * numvert);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.