Arghh passing 2D arrays to a func gives errors

This is a discussion on Arghh passing 2D arrays to a func gives errors within the C Programming forums, part of the General Programming Boards category; Hi C people, I'm trying to teach my self how to program in C and I'm generally new to programming. ...

  1. #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");
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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';
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

  4. #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");
    }

  5. #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

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #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.

  8. #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.

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

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

Tags for this Thread


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