Language C: Manipulating a vector of structs within functions

1

Hello, I created a struct :

typedef struct 
    {
        float valor;
        float peso;
    } objeto;

And inside main () I created a vector of these guys:

objeto conjunto[50];

Here, I need to exchange a 'place' object with another within the vector:

void ordena(objeto conjunto[],int n)
{
    int i, maior = conjunto[n].valor;
    for ( ; n > 1; n-- )
    {
        maior = selecMaior(conjunto, n );
        if (  conjunto[maior].valor > conjunto[n].valor )
            troca( &conjunto[n], &conjunto[maior]);
    }
}
void troca(objeto *a, objeto *b)
{
    objeto aux;
    aux.valor = a->valor;  
    aux.peso = a->peso;

    a->valor = b->valor;
    a->peso = b->valor;

    b->valor = aux.valor;
    b->peso = aux.peso;
}

Only Dev ++ is grumbling:

  

[Warning] conflicting types for 'swap'

(on the line of the call)

  

[Note] previous implicit declaration of 'exchange' was here

As the material I consulted would be right, and I already got a code like this working. Where I'm wrong

    
asked by anonymous 09.06.2016 / 17:04

1 answer

1
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).

    
10.06.2016 / 17:31