Can’t it be used as a function?

Asked

Viewed 32 times

1

I’m creating a program that sorts some numbers from a vector, so it’s a bubble sort. I’m using a function called trocar to exchange seat numbers when one is larger than the other, but the compiler indicates that I cannot use this function.

My code :

void trocar(int vetor[], int i) {
    int temp = vetor[0];
    vetor[0] = vetor[1];
    vetor[1] = temp;
}


int main() {
    setlocale(LC_ALL,"portuguese");

    int vetor[5],i,trocar,trocado = false;

    for(int i = 0; i < 5; i++) {
        if(vetor[i] > vetor[i+1]) {
            trocar(vetor[i],vetor[i+1]);
            trocado = true;
        }
    }


}

It indicates the following error :

'change' cannot be used as a function

How can I correct this mistake ?

1 answer

1


The error you are getting is due to the fact that you are using trocar as a function.

Every time you open keys {}, you define a new local scope, where you can reset the symbols that exist in a larger scope.

In that case, the identifier trocar inside the block is referring to a local variable, therefore you cannot use it as a function. trocar within the scope top-level is a function, but inside the block it is hidden (shadowed, in the English term) by defining the local variable with the same name. This means you can’t call the function trocar within that block (and any other sub-blocks thereof).

More precisely, you cannot access the function trocar by its name, but you can still call it by a pointer to that accessible function within that scope.

This should serve as an incentive to define meaningful names for their functions and other global symbols, since they will have to coexist in the scope top-level and are likely to be hidden by lower-range symbols.

To resolve, rename your local variable trocar or rename your function.

  • 1

    Thanks again merchant for helping me, this was a mistake I ended up letting go unnoticed.

Browser other questions tagged

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