Error when I try to call a function

Asked

Viewed 93 times

1

I need to calculate the distance between two points, but when I call the function that does this (calculates the distance), it gives the error: "subscripted value is neither array nor Pointer vector".

I did the same thing in another function. I needed to calculate the distance between two points and it worked. I don’t know why it doesn’t work now.

Distance function:

float distancia(int ax, int ay, int bx, int by)
{
    return (sqrt((pow((ax - bx), 2)) + (pow((ay - by), 2))));  
}

Function where it worked when I called 'distance'':

void mDistancia(char cidadeXY[][150], int coordenadasX[], int coordenadasY[], float matrizDistancia[][30])
{
    int c, i, contadorC, contadorI;
    float dij; 

    for(i = 0, contadorI = 0; i < 30; i++, contadorI += 2)
    {
        for(c = 0, contadorC = 0; c < 30; c++, contadorC += 2)
        {
            dij = distancia(coordenadasX[contadorI], coordenadasY[contadorI], coordenadasX[contadorC], coordenadasY[contadorC]);
            matrizDistancia[i][c] = dij;
        }
    }
}

Function where it did not work:

void problema5(char cidadeXY[][150], int coordenadasX[], int coordenadasY[])
{
    int i, j;
    float distanciaX, distanciaY;
    float dis, aux;
    int cX, cY;
    char cid[150];

    for(j = 0; j < 30; j++)
    {
        aux = distancia(coordenadasX[j], cX[j], coordenadasY[j], cY[j]);

        if(aux < dis)
        {
            dis = aux;
            strcpy(cid, cidadeXY[i]);
        }
    }
}

Notice that I did the same thing. I kept the value returned in the variable float aux, just as I did with the variable float dij. But only gives this error in function problem5. The error happens twice, more specifically in the j in coordinates Y[j] and Cx[j]. At least that’s what it says in the terminal.

1 answer

1

    int cX, cY;

        aux = distancia(coordenadasX[j], cX[j], coordenadasY[j], cY[j]);
        //                               ?????                   ?????

Nor cX nor cY are arrays. You can’t use it that way.

  • In fact, and in addition, the order of the parameters does not make any sense in the code.

  • 1

    Thank you! I didn’t realize it in the middle of that pile of code.

Browser other questions tagged

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