compiler warning help

Monado_III

Well-Known Member
OP
Member
Joined
Feb 8, 2015
Messages
722
Trophies
0
Location
/dev/null
XP
1,433
Country
Canada
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)
    ^
 

Attachments

  • program5_08.txt
    5.4 KB · Views: 216

spoonm

Can count to 3.
Member
Joined
May 20, 2015
Messages
192
Trophies
0
Website
spoonm.org
XP
299
Country
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?
 

Attachments

  • gbatemp.txt
    6.4 KB · Views: 205
Last edited by spoonm,
  • Like
Reactions: Monado_III

Monado_III

Well-Known Member
OP
Member
Joined
Feb 8, 2015
Messages
722
Trophies
0
Location
/dev/null
XP
1,433
Country
Canada
-insert large very helpful post here-

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.

I'm assuming you're making it in C++, as declarations inside for parameters doesn't work in C unless in C99 mode.
Nope, C. Your example complied in C as well as C++ (both times gave a Seg fault)

I see a Y before the crash, so you know where to look for bugs. Why does GBAtemp not let people upload .cpp files?
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.
 

spoonm

Can count to 3.
Member
Joined
May 20, 2015
Messages
192
Trophies
0
Website
spoonm.org
XP
299
Country
Brazil
-- Insert his post in here --

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.
 
  • Like
Reactions: Monado_III

Monado_III

Well-Known Member
OP
Member
Joined
Feb 8, 2015
Messages
722
Trophies
0
Location
/dev/null
XP
1,433
Country
Canada
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.
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()

I don't know that much C, like at all. I can do some simpler multi-dimensional array stuff but I have some trouble understanding actual programs. (as should be expected I guess, I'm only 14)
 

Attachments

  • program5_07.txt
    3.1 KB · Views: 200

spoonm

Can count to 3.
Member
Joined
May 20, 2015
Messages
192
Trophies
0
Website
spoonm.org
XP
299
Country
Brazil
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()

I don't know that much C, like at all. I can do some simpler multi-dimensional array stuff but I have some trouble understanding actual programs. (as should be expected I guess, I'm only 14)

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.
 

Monado_III

Well-Known Member
OP
Member
Joined
Feb 8, 2015
Messages
722
Trophies
0
Location
/dev/null
XP
1,433
Country
Canada
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.
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,

gudenau

Largely ignored
Member
Joined
Jul 7, 2010
Messages
3,855
Trophies
2
Location
/dev/random
Website
www.gudenau.net
XP
4,547
Country
United States
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. ;-)
 
  • Like
Reactions: Monado_III

Monado_III

Well-Known Member
OP
Member
Joined
Feb 8, 2015
Messages
722
Trophies
0
Location
/dev/null
XP
1,433
Country
Canada
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
 

Attachments

  • ttt.txt
    1.9 KB · Views: 185

spoonm

Can count to 3.
Member
Joined
May 20, 2015
Messages
192
Trophies
0
Website
spoonm.org
XP
299
Country
Brazil
No clue how that works, something I've always wonder 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.

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,
  • Like
Reactions: Monado_III

Monado_III

Well-Known Member
OP
Member
Joined
Feb 8, 2015
Messages
722
Trophies
0
Location
/dev/null
XP
1,433
Country
Canada
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
(no offence) What does this have to do with my current program? Are matrices involved in it?
 

spoonm

Can count to 3.
Member
Joined
May 20, 2015
Messages
192
Trophies
0
Website
spoonm.org
XP
299
Country
Brazil
(no offence) What does this have to do with my current program? Are matrices involved in it?

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.
 

Monado_III

Well-Known Member
OP
Member
Joined
Feb 8, 2015
Messages
722
Trophies
0
Location
/dev/null
XP
1,433
Country
Canada
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.
oh, okay. I thought you were linking it because it had some use in what I was doing.
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • JuanMena @ JuanMena:
    Will you give me mouth to mouth oxygen if my throat closes?
  • K3N1 @ K3N1:
    Nah the air can do that
  • K3N1 @ K3N1:
    Ask @x65943 he's trained for that stuff
  • JuanMena @ JuanMena:
    Kissing random dudes choking in celery? Really? Need to study for that?
  • K3N1 @ K3N1:
    Yes it requires a degree
  • K3N1 @ K3N1:
    I could also yank out the rest of my teeth but theirs professionals for that
  • x65943 @ x65943:
    If your throat closes, putting oxygen in your mouth will not solve anything - as you will be introducing oxygen prior to the area of obstruction
  • JuanMena @ JuanMena:
    Just kiss me Kyle.
  • x65943 @ x65943:
    You either need to be intubated to bypass obstruction or create a stoma inferior to the the area of obstruction to survive
  • x65943 @ x65943:
    "Just kiss me Kyle." And I thought all the godreborn gay stuff was a smear campaign
  • JuanMena @ JuanMena:
    If I die, tell my momma I won't be carrying Baby Jesus this christmas :sad::cry:
  • K3N1 @ K3N1:
    Smear campaigns are in The political section now?
  • JuanMena @ JuanMena:
    Chary! Chary! Chary, Chary, Chary!
  • Sonic Angel Knight @ Sonic Angel Knight:
    Pork Provolone :P
  • Psionic Roshambo @ Psionic Roshambo:
    Sounds yummy
  • K3N1 @ K3N1:
    Sweet found my Wii u PSU right after I ordered a new one :tpi:
  • JuanMena @ JuanMena:
    It was waiting for you to order another one.
    Seems like, your PSU was waiting for a partner.
  • JuanMena @ JuanMena:
    Keep them both
    separated or you'll have more PSUs each year.
  • K3N1 @ K3N1:
    Well one you insert one PSU into the other one you get power
  • JuanMena @ JuanMena:
    It literally turns it on.
  • K3N1 @ K3N1:
    Yeah power supplies are filthy perverts
  • K3N1 @ K3N1:
    @Psionic Roshambo has a new friend
    +1
  • JuanMena @ JuanMena:
    It's Kyle, the guy that went to school to be a Certified man Kisser.
  • Psionic Roshambo @ Psionic Roshambo:
    Cartmans hand has taco flavored kisses
  • A @ abraarukuk:
    hi guys
    A @ abraarukuk: hi guys