How to pass structure to function?

1
  

An agency in a country town has a maximum of 10,000 customers. Create an algorithm that can enter account number, name and balance of each client. The algorithm should print all accounts, their balances and one of the messages: positive / negative. The typing ends when you enter -999 for the account number or when you reach 10,000. At the end, the algorithm should show the total number of customers with negative balance, total agency customers and agency balance.)

Well, I soon thought of using a data structure with the necessary variables to store the requested information to the client N times, each time from a different client. Well, here's my code.

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>


    typedef struct clientes
    {
        char nome[50];
        int conta;
        int saldo;

    } clientes;


    struct clientes cadastro_cliente (clientes x);


    int main()
    {
        setlocale(LC_ALL,"");

        struct clientes x[1000];
        int i;

        for(i = 0; i < 5; i++)
        {
            cadastro_cliente(clientes x[i]);
        }
    }


    struct clientes cadastro_cliente (clientes x);
    {
        printf("Insira o nome completo do titular da conta: ");
        scanf("%s", &x.nome);

        printf("Insira o número da conta: ");
        scanf("%i", &x.conta);

        printf("Insira o saldo da conta: ");
        scanf("%i", &x.saldo);
    }

The code returns me an error while executing, an error already expected because I am not aware of how to pass a structure to a function that would be called N times. I came to this point watching a video on YouTube but can not be doubted. How would you pass a structure to function and call it at main() whenever requested by the user?

    
asked by anonymous 23.11.2017 / 21:06

1 answer

1

The best way to grow is to learn in a structured way. Mostly in C that is full of details. If in any language you have to know exactly how everything works, you can not think why it works is right, in C this is absurdly more important.

I do not know where you are learning, but watch out for volunteer sources who do not know and try to teach others. The internet is full of this. Here you are a little better because most subjects have other experienced people who evaluate the answers, yet nothing is 100% reliable.

The code has several errors and does not compile. I improved this way:

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>

typedef struct {
    char nome[50];
    int conta;
    int saldo;
} Cliente;

void cadastro_cliente(Cliente *cliente) {
    printf("\nInsira o nome completo do titular da conta: ");
    scanf("%49s", cliente->nome);
    printf("\nInsira o número da conta: ");
    scanf("%d", &cliente->conta);
    printf("\nInsira o saldo da conta: ");
    scanf("%d", &cliente->saldo);
}

int main() {
    setlocale(LC_ALL,"");
    Cliente clientes[1000] = {{ .nome = "", .conta = 0, .saldo = 0 }};
    for (int i = 0; i < 2; i++) {
        cadastro_cliente(&clientes[i]);
    }
    for (int i = 0; i < 2; i++) {
        printf("\n%s - %d", clientes[i].nome, clientes[i].conta);
    }
}

See working on ideone . And no Coding Ground . Also put it in GitHub for future reference .

Note that I made some subtle improvements, including naming, pay attention to each character.

The passage must be by reference in order for the data to be placed in the structure. Normally a structure is a type by value and copies the data having another object, when it passes the structure reference there is no copy and for all intents and purposes the structure is in the original location that it was created, so in main() in this code. It is the same process used in scanf() , which by the way one of them was wrong.

I preferred to initialize all elements of the array . It's a bit slower, but it's safer. It depends on a number of factors to choose what is best.

Be careful because this works, but if you change some things this code may have other problems that will not interact well. There are a lot of things in the code that work as basic learning, but in real code it would not be done like that (one of the reasons I do not like these courses on YouTube, at least they should warn you that's not how you actually do it).

Look here for more on the subject, there is a lot already evaluated.

    
23.11.2017 / 21:42