Can not be used as a function?

1

I'm creating a program that sorts some numbers of a vector, and then is a bubble sort. I'm using a function called trocar to change place numbers when one is larger than the other, but the compiler indicates that I can not 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:

  

'swapping' can not be used as a function

How can I fix this error?

    
asked by anonymous 19.06.2017 / 02:02

1 answer

1

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

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

In this case, the trocar identifier within the block is referring to a local variable, so you can not use it as a function. trocar in the top-level scope is a function, but within the block it is hidden ( shadowed ) by setting the local variable with the same name . This means that you can not call the trocar function within that block (and any other sub-blocks thereof).

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

This should serve as an incentive to define meaningful names for their functions and other global symbols, as they will have to coexist at the top-level scope and run the risk of being hidden by lower range.

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

    
19.06.2017 / 02:19