How to pass structure to function?

Asked

Viewed 100 times

1

An agency in a small town has a maximum of 10,000 clients. Develop an algorithm that can enter each client’s account number, name and balance. The algorithm must print all accounts, their balances and one of the messages: positive / negative. Typing ends when you type -999 for the account number or when it reaches 10,000. At the end, the algorithm should show the total of clients with negative balance, the total of clients of the agency and the balance of the agency.)

Well, I soon thought of using a data structure with the necessary variables to store the information requested from 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 when executing, an expected error because I have no knowledge of how to pass a structure to a function that would be called N times. I got to this point watching a video on Youtube but I can not ask questions. How would pass a structure to function and call the same in main()whenever requested by the user?

1 answer

1


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

I don’t know where you’re learning from, but beware of willful sources who don’t know and try to teach other people. The internet is full of it. Here you are a little better because in most subjects have other experienced people who evaluate the answers, yet nothing is 100% reliable.

The code has several errors and does not even compile. I improved so:

#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 in the repl it.. Also put on the Github for future reference.

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

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

I preferred to initialize all elements of array. It gets a little slower, but it’s safer. It depends on a number of factors to choose the one that’s best.

Be careful because that’s how it works, but if you change a few things that code can have other problems that won’t interact well. There are many things in the code that work as basic learning, but in real code would not be done like this (one of the reasons I do not like these courses on Youtube, at least should warn that it is not how it is done in reality).

Look here more on the subject, there is enough already evaluated.

Browser other questions tagged

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