0

i have this function:

void Win_Event(int player, int x, int y, int mat[][MAX]) {
    int choice, i, j;
    for (i = 0; i < y; i++) {
        for (j = 0; j < x; j++) {
            printf("\033[1;31m");
            if (mat[i][j] == -1)
                printf("\033[0;32m");
            else if (mat[i][j] == 0)
                printf("\033[0;35m");
            printf("%3d |", mat[i][j]);
        }
        printf("\n");
    }
    printf("\033[1;31m");
    printf("\n\033[0;36mPlayer %d Won!\nWould you like to replay? (1 -> yes, 2-> no):", player);
    scanf_s("%d", &choice);
    main(choice);
}

it basically checks if the player won then asks him if he wants to replay the game and sends that input back to the main function. the main function looks like this:

void main(int choice) {
    printf("\033[0;33m");
    printf("---Welcome To Pente---\n");
    if (choice == 1)
        SetBoardSize();
    else
        return 0;
}

the program just ignored the choice parameter completely and runs the SetBoardSize function straight away is there something i am missing?

Punzicul
  • 51
  • 1
  • 5
  • BTW even if i make the main an int type function it dosent work – Punzicul Feb 02 '22 at 14:54
  • 1
    It's a poor choice to recursively call `main()`. Usually, you `return` back to the caller, which could be `main()`, where the program flow continues. And you can't pass your `choice` to `main()` in that way anyway. – Weather Vane Feb 02 '22 at 14:57
  • 1
    "and sends that input back to the main function" This is not how functions work. What this code does is *call `main` again*, in a *completely separate context* from the original call. Incidentally, it is not technically allowed to call `main` yourself. – Karl Knechtel Feb 02 '22 at 14:57
  • calling a function that gets choice as a parameter and then returns 0 if the value means it needs to end dosent work too. so i dont think the main problem is with the main function. additionaly i printed the choice value and it is 2 when it gets passed but it completely ignores the if statements and just calls the function. – Punzicul Feb 02 '22 at 15:06
  • 1
    I recommend you to begin with `int main(int argc, char *argv[])` and make your first job to figure out what the program agument was. It most certainly is not what you have done. Forget about everying else until you can take the user's choice correctly, because you have run ahead of yourself. – Weather Vane Feb 02 '22 at 15:09
  • 2
    @KarlKnechtel It's usually bad design, but calling main recursively is allowed. – klutt Feb 02 '22 at 15:10
  • That is [not my understanding](https://stackoverflow.com/questions/4518598/is-it-legal-to-recurse-into-main-in-c). – Karl Knechtel Feb 02 '22 at 15:13
  • 2
    @KarlKnechtel that link is C++ not C. Please see [this](https://stackoverflow.com/questions/18446686/main-function-in-c#comment92808500_18446831) – Weather Vane Feb 02 '22 at 15:15
  • @WeatherVane: I once saw `signal(SIGINT, main);` in real production code :-) – Steve Friedl Feb 02 '22 at 15:50
  • 1
    @SteveFriedl Thank you very much. My head just exploded. – Steve Summit Feb 02 '22 at 16:04
  • Data entry app, user could control-C to restart, it's worked for decades. Still blows me away that anybody could think about this. – Steve Friedl Feb 02 '22 at 16:14

2 Answers2

1

main() is a special kind of function in C, with special rules.

It always the compiler and never the programmer that decides what forms of main() that are supported.

If your compiler does not support void main(int choice) (which it likely does not), then you cannot use that form, period. If you wish to deviate from the two standardized forms int main (void) and int main (int argc, char* argv[]), then you must read the compiler's documentation about implementation-defined forms to see what it does support (if any). You can't just cook up something yourself.

In addition, calling main() manually is allowed in C, but it is a very bad idea and there exists no sensible scenario where you should do that. It will cause you to stack a bunch of extra overhead memory recursively.

Lundin
  • 195,001
  • 40
  • 254
  • 396
0

First I suggest you not to recur on main(), because it isn't a good coding practice and you don't have any reason to do that (move the recursive part of your program in an another function). Second if you really want to use the same code design of now and you need to exit when return 0 is called, you should replace it with:

exit(EXIT_SUCCESS);

from stdlib.h

Moreover, remember that recursion can lead to an stack overflow, take care of it.

Marco Valle
  • 176
  • 6