C Language: Manipulating a vector of structs within functions

Asked

Viewed 594 times

1

Hello, I created a struct:

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

And within the main() I’ve created a vector of these guys:

objeto conjunto[50];

Then I need to swap one 'object' of place 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 muttering:

( in the exchange header() )

[Warning] Conflicting types for 'swap'

( on the call line )

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

As the material I consulted would be right, and I also got a code like this working. Where I’m missing

  • Put all your code so you can see the problem. [mcve]

  • Change of place (position) the functions orders and exchange (exchange comes first)

  • Thank you for the editing, I will do so in my next doubts.

1 answer

2


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

  • Thank you! Before your explanation I had no idea why some codes have such prototypes.

  • Cool - I complemented a little more the answer - give a read.

Browser other questions tagged

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