The problem is only that the compiler arrives at the so-called "exchange" function in its "main" function before "seeing" the "exchange" statement - then assumes it is a function that returns a int
(the pattern in C), and simply puts the parameters in the call, without having the slightest idea whether they are correct or not.
The way to solve this is to declare the "exchange" 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 function declaration, but terminated with a ;
, instead of a {
that would initiate the function itself.
So, in your file, you’d put:
void troca(objeto *a, objeto *b);
...
void ordena(...) {
...
}
...
void troca(objeto *a, objeto *b) {
...}
And it’s all right.
In general, prototypes are placed in the archives .h
in larger programs in C - besides allowing access to functions independent of the order in which they were declared, are the way to allow the use of functions that are in other files .c
on your system, or even in libraries apart: in fact, they are the main reason why it is necessary to include the files ". h" - when someone type #include <stdio.h>
- stdio. h does not have the code of functions printf, scanf, etc... What it has are the prototypes - then the compiler knows how to assemble the calls to these functions, and in a next, binding step of the object, which can be at runtime, is that the code of these functions is executed: their C-coded does not 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 like "jpg" - your computer needs to have "libjpeg" installed - only the binaries = but to compile your project you need to have the files ". h" from that library. that is why, for instance, in several Linux distributions there exists in separate packages a library (e.g., "libjpeg") - which contains only the already compiled code of that library, the development files of that library (e.g.: " libjpeg-dev") - which contains the files ". h" with the prototypes of the public functions, and the source code of the library (e.g. "libjpeg-src"). Whichever one will only isntalar programs already compiled, only need the first. Who will compile programs (yours or others) that will manipulate JPEG files, need both first - and only need the complete fotne code who wants to study how the library works inside, or search for the fotne of some bug relcionado anyway. (Who wants to contribute to the development of the library, then already takes the library from its versioning repository, not the package of a specific distribution).
Put all your code so you can see the problem. [mcve]
– Maniero
Change of place (position) the functions orders and exchange (exchange comes first)
– user5299
Thank you for the editing, I will do so in my next doubts.
– Ricardo Ireno