Good practices in C on "main ()" call?

0

I'm developing a simple application for college work, and I'm hesitant to use the main() function in code so instead of going back to the beginning of the program, would that be a good practice or not?

Below I have an example in my function cadastro() , notice that at the end of the program I put main() , in case the user type 4, 5 or any other key, so that it does not exit the program. p>

void cadastro(){
    int op;
    static int linha;

    cabecalhoCadastrar();

    do{
        //printf(":::::Cadastrar cliente:::::\n\n");

        if(linha >= 5){
            printf("\nAgenda lotada!\n");
            break;
        }
        else{
            printf("\nNome: ");
            fflush(stdin);
            fgets(cliente[linha].nome, 50, stdin);

            printf("Telefone: ");
            fflush(stdin);
            scanf("%d", &cliente[linha].telefone);

        /*  printf("Endereço: (rua, número, complemento): ");
            fflush(stdin);
            fgets(cliente[linha].endereco, 150, stdin);*/

            linha++;

            printf("\nCliente cadastrado com sucesso!\n\n\n");
            system("pause");
            system("cls");

            cabecalhoCadastrar();
            printf("\n1- Cadastrar cliente\n2- Sair\n");

            fflush(stdin);
            scanf("%d", &op);
            system("cls");
        }
    }while(op == 1);
    main();
}
    
asked by anonymous 01.07.2018 / 21:37

1 answer

1

As I say: forget good practices. You can only follow them without causing damage who does not need them, which is a paradox.

It's a question of whether or not it's right for the context.

This particular case is wrong. It may even work in the exercise, but if you enter the main() function as you are doing it will accumulate in the stack and this will cause a memory leak at some point.

Depending on other factors it may even cause other problems, this is the worst thing you can do, an example is when you use static which is almost never adequate, especially in this case where recursive calls inadvertently. >

When function execution ends function should only return air to the calling function. You should never call the calling function or another function that can call the calling function to avoid an unwanted loop and stack overflow.

You have several other errors in this application.

    
01.07.2018 / 21:45