compiler warning help

Discussion in 'Computer Programming, Emulation, and Game Modding' started by Monado_III, Aug 30, 2015.

  1. Monado_III
    OP

    Monado_III GBAtemp Advanced Fan

    Member
    636
    349
    Feb 8, 2015
    Canada
    /dev/null
    Can anyone help me figure out what these compiler warnings are talking about? I have a very limited knowledge atm of how to use pointers and I'm not sure how I would fix these, which, from what I can tell, are what is causing the executable to have a segmentation fault. The C file is posted at the bottom, I can also post the core dumped from the segmentation fault if anyone wants to look at it (as soon as I change the directory names in the core to hide my actual name :P).
    Code:
    program5_08.c: In function ‘minimax’:program5_08.c:27:27: warning: passing argument 2 of ‘win’ from incompatible pointer type [-Wincompatible-pointer-types]
    int winning = win(board, &winner, player);
                              ^
    program5_08.c:5:5: note: expected ‘int *’ but argument is of type ‘int **’
    int win(char board[3][3], int* winner, int player)
        ^
    program5_08.c:40:40: warning: passing argument 3 of ‘minimax’ from incompatible pointer type [-Wincompatible-pointer-types]
        int thisScore = -minimax(board, 2, &winner);
                                           ^
    program5_08.c:24:5: note: expected ‘int *’ but argument is of type ‘int **’
    int minimax(char board[3][3], int player, int* winner)
        ^
    program5_08.c: In function ‘computer_move’:
    program5_08.c:69:40: warning: passing argument 3 of ‘minimax’ from incompatible pointer type [-Wincompatible-pointer-types]
        int tempScore = -minimax(board, 2, &winner);
                                           ^
    program5_08.c:24:5: note: expected ‘int *’ but argument is of type ‘int **’
    int minimax(char board[3][3], int player, int* winner)
        ^
    
     

    Attached Files:

  2. spoonm

    spoonm Can count to 3.

    Member
    174
    84
    May 20, 2015
    Brazil
    In line 27, inside minimax(), you pass &winner(the address of the local variable winner) to win. You are passing the address of a pointer to int, which is going to be of type pointer to pointer to int(int **). In win(), you are expecting an argument of type int *, not int **. What you probably meant to pass is winner itself, given the argument you received named winner is already of type int *.

    The same is happening elsewhere, and that'll give you all those warnings. I hope you understand what I'm saying.

    What you're meant to be doing is pass the int* winner you got as parameters in the functions you first called.

    Example:
    Code:
    int win (char board[3][3], int* winner, int player)
    {
        ...
    }
    
    ...
    
    int minimax (char board[3][3], int player, int* winner)
    {
        int winning = win(board, winner, player);
        ...
    }
    winner, local variable in minimax(), is of type int*, which win() expects as its second argument.

    EDIT:

    I have made a few edits to your source code and added observations. I know I'll sound like a nitpicking jerk with what I placed there, but please give it some thought.

    I'm assuming you're making it in C++, as declarations inside for parameters doesn't work in C unless in C99 mode. I compiled through g++ and got no warnings or errors, but when playing against the computer, the program crashes. I see a Y before the crash, so you know where to look for bugs.

    Why does GBAtemp not let people upload .cpp files?
     

    Attached Files:

    Last edited by spoonm, Sep 3, 2015
    Monado_III likes this.
  3. Monado_III
    OP

    Monado_III GBAtemp Advanced Fan

    Member
    636
    349
    Feb 8, 2015
    Canada
    /dev/null
    thanks for actually looking through all that! For your comment about the computer choice do-while loop, I just noticed how I only expected it to go once, not sure why I thought I should put it in a loop. And I haven't actually leant a whole lot about pointers yet but it makes sense that what I was doing was causing the warnings.

    Nope, C. Your example complied in C as well as C++ (both times gave a Seg fault)

    I don't :wink: (when compiled as C AND C++)
    IDK, they really should seeing as how they have a sub-forum that's for programming and programming related things.
     
  4. spoonm

    spoonm Can count to 3.

    Member
    174
    84
    May 20, 2015
    Brazil
    I believe what's causing the segfault is computer_move(), given that's right after printf("Y"). I'm sorry I only skimmed through it and made those changes, I didn't stop to think about how it all worked, so I don't have good advice for this.

    In computer_move(), why do you loop like this: for(int i = 0; i < 2; i++) and not for(int i = 0; i < 3; i++)?
    I'd assume you wanted to loop throught he entired board, which is 3x3. That's about all I can say, sorry. If you want me to look at the code, tell me and I'll stop to study it a bit. I'd appreciate it if you added a few comments here and there to better explain what's going on. Don't bother if you don't feel like it, though.
     
    Monado_III likes this.
  5. Monado_III
    OP

    Monado_III GBAtemp Advanced Fan

    Member
    636
    349
    Feb 8, 2015
    Canada
    /dev/null
    Before I do that I should add what I'm doing probably is a really crappy idea but what I wanted to do was add a 1Player 'mode' to a program in my book (see uploaded file), but when I looked around on the internet I found that it was relatively simple to make an unbeatable tic-tac-toe AI, but I couldn't find any C examples except for this one. So I thought since adding a 1Player mode to the file below would be more challenging then simply adding a 2 player mode to that, I thought I'd give it a try and see what I'd learn (aka error messages, how stuff works).

    So I literally copy pasted the minimax and computer move functions from that github one into the program below, as expected it gave tons of errors, but I did lean what several errors/warnings are and then fixed them and replaced variables accordingly. So I don't actually don't know that much of what is happening outside of int main()

    Warning: Spoilers inside!
     

    Attached Files:

  6. spoonm

    spoonm Can count to 3.

    Member
    174
    84
    May 20, 2015
    Brazil
    I suggest keeping main() short and doing the rest through functions, which should be organized in multiple .c files, using header files to include them.

    I'll give this a look and see what comes out of it. It's cool you're taking the time to learn it.
     
  7. Monado_III
    OP

    Monado_III GBAtemp Advanced Fan

    Member
    636
    349
    Feb 8, 2015
    Canada
    /dev/null
    No clue how that works, something I've always wonderd tbh, how does one C file 'talk' to another one during run time, does the compiler just take both C files and make one executable or ?

    thanks for helping me btw.
     
    Last edited by Monado_III, Sep 3, 2015
  8. gudenau

    gudenau Never a unique idea

    Member
    3,257
    1,224
    Jul 7, 2010
    United States
    /dev/random
    Copy the stuff into a diffrent source file, make a header with the same name with function declerations, include the header in the source and anywhere you use the functions. Sewms kinda complicated but you will get the hang of it. ;-)
     
    Monado_III likes this.
  9. Monado_III
    OP

    Monado_III GBAtemp Advanced Fan

    Member
    636
    349
    Feb 8, 2015
    Canada
    /dev/null
    So my header file should look something like this?

    edit: it's just the first three functions from the "gbatemp.txt" file @spoonm uploaded in between a
    #ifndef ttt
    #define ttt
    and an
    #endif
     

    Attached Files:

    • ttt.txt
      File size:
      1.9 KB
      Views:
      50
  10. spoonm

    spoonm Can count to 3.

    Member
    174
    84
    May 20, 2015
    Brazil
    Like gudenaurock said, you use header files. The compiler will receive all your .c files, here's an example:

    main.c
    win.c
    win.h

    main.c:
    Code:
    #include <stdio.h>
    #include "win.h"
    
    int main ()
    {
        printf("Output of hw(3): %d\n", hw(3));
    }
    win.c:
    Code:
    #include <math.h>
    #include "win.h"
    
    int hw (int in)
    {
        return (2*in + pow(in, 2))%4;
    }
    win.h:
    Code:
    #ifndef WIN_H
    #define WIN_H
    
    int hw (int in);
    
    #endif
    To compile from the command line: gcc main.c win.c -o program

    Once you run program(or on Windows, program.exe), you'll see:
    "Output of hw(3): 3"

    EDIT #1:

    Yes, that would work, but it's not common practice to keep actual code in header files.

    EDIT #2:

    Here's my progress on reorganizing the last program you posted. This is far from finished, but I'm about to watch the season finale of Mr. Robot. :P

    Hopefully this is useful. I have a C project using matrices that I started for my Programming Logic II class, but stopped working on it before it was finished. I had a rough prototype done beforehand, and it worked beautifully, but wanted to refactor it and ended up deleting the one that worked fine.

    Here's a link to the repo(development branch): https://github.com/skewerr/mtrcop/tree/develop
     
    Last edited by spoonm, Sep 3, 2015
    Monado_III likes this.
  11. gudenau

    gudenau Never a unique idea

    Member
    3,257
    1,224
    Jul 7, 2010
    United States
    /dev/random
    You should be able to put #pragma once at the top of the headers instead of the def stuff.

    Edit:
    When you get to structures, put those in the headers as well.
     
    Monado_III likes this.
  12. spoonm

    spoonm Can count to 3.

    Member
    174
    84
    May 20, 2015
    Brazil
    Just noticed I forgot to upload the file. My bad. Here.

    By the way, the season finale was "ehhhh okay".
     

    Attached Files:

    Monado_III likes this.
  13. Monado_III
    OP

    Monado_III GBAtemp Advanced Fan

    Member
    636
    349
    Feb 8, 2015
    Canada
    /dev/null
    thanks
     
  14. Monado_III
    OP

    Monado_III GBAtemp Advanced Fan

    Member
    636
    349
    Feb 8, 2015
    Canada
    /dev/null
    (no offence) What does this have to do with my current program? Are matrices involved in it?
     
  15. spoonm

    spoonm Can count to 3.

    Member
    174
    84
    May 20, 2015
    Brazil
    You said you didn't understand how .c files could "communicate with one another". That project makes use of multiple header and .c files and I thought you could learn something from it. Otherwise it has nothing to do with your current program, sorry.
     
  16. Monado_III
    OP

    Monado_III GBAtemp Advanced Fan

    Member
    636
    349
    Feb 8, 2015
    Canada
    /dev/null
    oh, okay. I thought you were linking it because it had some use in what I was doing.