The problem is just that the compiler arrives at calling the function "swap" in its "main" function before "seeing" the "switch" statement - then assumes that it is a function that returns a
int
default in C), and simply put the parameters in the call, without having the faintest idea whether they are correct or not.
The way to solve this is to declare the "switch" function at the beginning of the file, before the function main
- not necessarily moving the body of the function there - C allows the use of prototypes (prototypes) that are exactly identical to the declaration of the function, but terminated with a ;
, instead of a {
that would start the function itself.
So, in your file, you would put:
void troca(objeto *a, objeto *b);
...
void ordena(...) {
...
}
...
void troca(objeto *a, objeto *b) {
...}
And it's okay.
In general, prototypes are placed in .h
files in larger programs in C - besides allowing access to functions independent of the order in which they were declared, they are the way to allow the use of functions that are in other .c
on your system, or even on separate libraries: in fact, they are the main reason you need to include the ".h" files - when someone types #include <stdio.h>
- stdio.h does not have the function code printf, scanf , etc ... What it has are prototypes - there the compiler knows how to mount calls to those functions, and in a next step, object binding, which may be at runtime, is that the code of these functions is run: The C-style of them does not even need to be present on the computer where you compile the program.
In an environment where you are compiling complex software, for example, to manipulate images of type "jpg" - your computer must have "libjpeg" installed - only the binaries = but to compile your program you need to have the ".h" files in that library. (for example, "libjpeg") - which only contains the code already compiled from that library, the development files of that library (eg "libjpeg- dev ") - which contains the" .h "files with the public function prototypes, and the library source code (eg" libjpeg-src "). Anyone who just goes to install programs already compiled, just needs the first one. Who will compile programs (his or her third ones) that will handle JPEG files, needs the first two - and only needs the complete code who wants to study how the library works inside, or search for some bug related to it. (Who wants to contribute to the development of the library, there it takes the library of the versioning repository of it, not the package of a specific distribution).