C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-12-2009, 06:48 AM   #1
Registered User
 
Join Date: Sep 2009
Posts: 9
Arghh passing 2D arrays to a func gives errors

Hi C people,

I'm trying to teach my self how to program in C and I'm generally new to programming. I've written some small programs with loops so far and now I'm trying to make a checkers game as my first real program in C.

I'm currently having trouble passing a two dimensional array to another function. I keep getting the error

"warning: assignment makes integer from pointer without cast"

???

Obviously I can't ignore this error, since when I fill up my 2D array with checkers pieces, I'm getting jibberish instead.

can someone see where I'm going wrong?

Btw I'm using context and cygwin.

Code:
#include <stdio.h>
#include <stdlib.h>

/*
A Simple checkers game
By Bryce M
ALG:
step 1 declarations
step 2 print checkers board
*/

void fillboard(char checkerboard[8][8])
{
 int i=0,j=0;
 for(i=0;i<8;i++){
   for(j=0;j<8;j++){
    if(i<3){
        // populate enemy board
       if(i%2 == 0){
         if(j%2 == 0){
          (checkerboard[i][j] = " ";
           // blanks every even o,2,
         }
         if(j%2 ==1){
         }
           checkerboard[i][j]= "O";
           //token on odd
         }
       if(i%2 == 1){
         if(j%2 == 0){
           checkerboard[i][j] = "O";
           // blanks every even o,2,
         }
         if(j%2 ==1){
           checkerboard[i][j]= " ";
           //token on odd
         }
       }

    }
    if(i<5 && i>3)
    {checkerboard[i][j] = " ";} //blanks in middle
    if(i>4)
    {
       // populate user board
       if(i%2 == 0){
           if(j%2 == 0){
             checkerboard[i][j] = " ";
             // blanks every even o,2,
           }
           if(j%2 ==1){
           }
             checkerboard[i][j]= "X";
             //token on odd
           }
       if(i%2 == 1){
           if(j%2 == 0){
             checkerboard[i][j] = "X";
             // blanks every even o,2,
           }
           if(j%2 ==1){
             checkerboard[i][j]= " ";
             //token on odd
           }
       }
    }// end i>4

    }//end j
   }//end i
 }//end function

void drawboard(char checkerboard[8][8])
{
  int i,j,n;
    for(n=0; n<9; n++)
    {
      if(n == 0){printf(" ");}
      else{printf(" %d",n);}
    }
    printf("\n");
    //draw row
    for(i=0; i<8; i++){
             for(j=0;j<18;j++){
                if(j ==0)
                { printf("%c",65+i);}
                else if(j%2 == 1)
                { printf("|");}
                else if(j%2 == 0)
                { printf("%c",checkerboard[i][j]);}
             }
             printf("\n");
    }
}

int main (int argc, char *argv[])
{
  char checkerboard[8][8];

  printf("\nRunning Checkers game v1.0\nBy brcye M\n");
  printf("Latest Rev 12/09/2009\n\n\n");

  printf("******************************\n");
  printf("** WELCOME TO CHECKERS IN C **\n");
  printf("******************************\n\n");
  fillboard(checkerboard);
  drawboard(checkerboard);

    return 0;
    printf("\n");
}
bryce is offline   Reply With Quote
Old 09-12-2009, 07:00 AM   #2
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
The way you are passing parameters for the char matrix is fine.

You are getting a bunch of those "warning: assignment makes integer from pointer without cast", one for each of these lines:
Code:
checkerboard[i][j]= "O";
Double quotes are for strings (hence the "pointer without a cast"). Single char values (the "integer") use single quotes:
Code:
checkerboard[i][j]= 'O';
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Old 09-12-2009, 07:54 AM   #3
Registered User
 
Join Date: Sep 2006
Posts: 2,504
In this part of the drawboard function, you are running out of the array boundary:

Code:
    for(i=0; i<8; i++){
             for(j=0;j<18;j++){
                if(j ==0)
                { printf("%c",65+i);}
                else if(j%2 == 1)
                { printf("|");}
                else if(j%2 == 0)
                { printf("%c",checkerboard[i][j]);}
             }
             printf("\n");
    }
When j becomes > 7, that's a serious error. Here, it goes all the way up to 17.

Your board will look a lot better, if you have each square be 3 char's wide, by 3 char's high, and always print the X or 0, into the very center of each square.

How to do that?

First, I'd print the board up, without any pieces - just one time. If you re-draw the squares after each move, the screen will start flickering or flashing, and look bad.

Second, when a piece is moved only two sqr's get re-drawn. The from square and the square the piece moves to. None of the rest of the squares are re-drawn.

The center of each square can be figured by working out a simple formula for it's position, on the screen:

lm = left margin is any spaces before the leftmost square.
square x location = lm + (square width of each square * column number of the square)

tm = top margin is any spaces between the top of the screen, and the top row of the board.

square y location = tm + (square height * the row number of the square)

Hope that makes sense.
Adak is offline   Reply With Quote
Old 09-12-2009, 08:27 AM   #4
Registered User
 
Join Date: Sep 2009
Posts: 9
wow that was fast!

Thanks for the quick replies.

I've fixed the array boundaries - I guess thats why I kept going over.

Here is what I have so far.

Code:
#include <stdio.h>
#include <stdlib.h>

/*
A Simple checkers game
By Bryce M

ALG:
step 1 declarations
step 2 print checkers board
*/

void fillboard(char checkerboard[8][8])
{
 int i=0,j=0;
 for(i=0;i<8;i++){
   for(j=0;j<8;j++){
    if(i<3){
        // populate enemy board
       if(i%2 == 0){
              if(j%2 == 0){
              checkerboard[i][j] = ' ';
              // blanks every even o,2,
              }
              if(j%2 ==1){
              checkerboard[i][j]= 'O';
              //token on odd
              }
       }
       if(i%2 == 1){
              if(j%2 == 0){
              checkerboard[i][j] = 'O';
              // token on even
               }
              if(j%2 ==1){
              checkerboard[i][j]= ' ';
              //blanks every odd 1,3,
              }
       }

    }
    if((i==3) || (i==4))
    {checkerboard[i][j] = ' ';} //blanks in middle
    if(i>4)
    {
           // populate user board
           if(i%2 == 0){
                  if(j%2 == 0){
                  checkerboard[i][j] = ' ';
                  // blanks every even o,2,
                  }
                  if(j%2 ==1){
                  checkerboard[i][j]= 'X';
                  //token on odd
                  }
           }
       
           if(i%2 == 1){
                  if(j%2 == 0){
                  checkerboard[i][j] = 'X';
                  // blanks every even o,2,
                   }
                  if(j%2 ==1){
                  checkerboard[i][j]= ' ';
                  //token on odd
                  }
           }
    }// end i>4

    }//end j
   }//end i
 }//end function

void drawboard(char checkerboard[8][8])
{
  int i,j,n, k=2;
    for(n=0; n<9; n++)
    {
      if(n == 0){printf(" ");}
      else{printf(" %d",n);}
    }
    printf("\n");
    //draw row
    for(i=0; i<8; i++){
             for(j=0;j<18;j++){
                if(j ==0)
                { printf("%c",65+i);}
                else if(j%2 == 1)
                { printf("|");}
                else if(j%2 == 0)
                { printf("%c",checkerboard[i][j-k++]);}
             }
             printf("\n");
    }
}

int main (int argc, char *argv[])
{
  char checkerboard[8][8];

  printf("\nRunning Checkers game v1.0\nBy Bryce M\n");
  printf("Latest Rev 12/09/2009\n\n\n");

  printf("******************************\n");
  printf("** WELCOME TO CHECKERS IN C **\n");
  printf("******************************\n\n");
  fillboard(checkerboard);
  drawboard(checkerboard);

    return 0;
    printf("\n");
}
bryce is offline   Reply With Quote
Old 09-12-2009, 08:43 AM   #5
Registered User
 
Join Date: Sep 2009
Posts: 9
on second thought - wouldn't it be easier to have one 2D character array with all pieces, numbers, letters, bars etc - then drawing it with 2 for loops every time would be easier?

Brb redoing entire code :P
bryce is offline   Reply With Quote
Old 09-12-2009, 09:05 AM   #6
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
Quote:
Originally Posted by bryce View Post
on second thought - wouldn't it be easier to have one 2D character array with all pieces, numbers, letters, bars etc - then drawing it with 2 for loops every time would be easier?

Brb redoing entire code :P
Not quite sure what you mean, but if you were thinking of combining the two functions into one, I would say hold off for a while.

Having several shorter functions that do each do fewer specific things is usually easier to work with than one longer one which does more things. That is not a hard and fast rule, but I would say if the two functions really can be combined, then it is just as easy to do that at the end (like polishing a final draft) as earlier, since the function calls will all occur the same way, one after the other or whatever. If, at the end, they DON'T always occur the same way, that is a big clue that you have, in fact, made use of the added "functionality", so you can leave them discrete.
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Old 09-12-2009, 09:10 AM   #7
Registered User
 
Join Date: Sep 2009
Posts: 9
ahhh,

thanks for that.

I was implying - filling an 18x19 array up with all the numbers 1-8, letters A-H, and all the pieces - that way when printing the array out it can be done with two nested - for loops. So it looks like

Code:
   1 2 3 4 5 6 7 8 
A | |0| |o| |0| |0| 
B |0| |0| |0| |0| | 
C | |0| |0| |0| |0| 
D | | | | | | | | | 
E | | | | | | | | | 
F |x| |x| |x| |x| | 
G | |x| |x| |x| |x| 
H |x| |x| |x| |x| |
I've started this but things have become much more complicated and the function looks huge - it has way too many if else statements.
I'm going back to the original

Last edited by bryce; 09-12-2009 at 09:37 AM.
bryce is offline   Reply With Quote
Old 09-12-2009, 09:27 AM   #8
Registered User
 
Join Date: Sep 2009
Posts: 9
i edited the code and tested the fillarray function - and it produces the correct output

but I the draw array seems to not work here

Code:
//draw row
    for(i=0; i<8; i++){
             for(j=0;j<18;j++){
                if(j ==0)
                { printf("%c",65+i);}
                else if(j%2 == 1)
                { printf("|");}
                else if(j%2 == 0)
                { printf("%c",checkerboard[i][j-k++]);}
             }
             printf("\n");
    }
}
which gives an output of

Code:
   1 2 3 4 5 6 7 8 
A | |0| |o| |0| |0| 
B | |0| |o| |0| |0| 
C | |0| |0| |0| |0| 
D | |0| |o| |0| |0| 
E | |0| |o| |0| |0|
F | |0| |o| |0| |0| 
G | |0| |o| |0| |0| 
H | |0| |o| |0| |0|
seems like its only printing the first row of the array.
some head scratching to ensue!

Last edited by bryce; 09-12-2009 at 09:38 AM.
bryce is offline   Reply With Quote
Old 09-12-2009, 11:24 AM   #9
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
For every iteration of the loop:

j++
k++

Thus your evaluation of j-k will remain the same, right? Presume we start with k=1.

j = 0
j-k = -1 (then k++, j++)
j = 1, k = 2
j-k = -1
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Reply

Tags
checkers, game, noob

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
2D Array's, assigning chars. gman89 C Programming 9 04-26-2008 11:03 PM
Help Understanding Passing Arrays To Functions jrahhali C++ Programming 7 04-10-2004 02:57 PM
returning 2D arrays ... C++ Programming 2 09-02-2003 12:28 PM
pointer to pointer and 2D arrays rfbu C Programming 6 06-23-2002 08:36 PM
errors with arrays and structures ssjnamek C++ Programming 4 03-03-2002 11:48 PM


All times are GMT -6. The time now is 10:28 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22