Hi!!! I'm trying to pass 2d array to function and i can't.
In general i'm trying to print chess board in action.
The board is an integer.I need a function that gets 2d array in specific place and returns correlatedm char.

const int pawn    = 1;
const int rook    = 2;
const int knight  = 3;
const int bishop  = 4;
const int queen   = 5;
const int king    = 6; 
)

my attemps:

#include<stdio.h>
int board[8][8];
int x;
int y;
void setup_board()
{
  // Normal board setup
  board[0][0]=2;
  board[0][1]=3;
  board[0][2]=4;
  board[0][3]=5;
  board[0][4]=6;
  board[0][5]=4;
  board[0][6]=3;
  board[0][7]=2;
  board[1][0]=1;
  board[1][1]=1;
  board[1][2]=1;
  board[1][3]=1;
  board[1][4]=1;
  board[1][5]=1;
  board[1][6]=1;
  board[1][7]=1;
  board[2][0]=0;
  board[2][1]=0;
  board[2][2]=0;
  board[2][3]=0;
  board[2][4]=0;
  board[2][5]=0;
  board[2][6]=0;
  board[2][7]=0;
  board[3][0]=0;
  board[3][1]=0;
  board[3][2]=0;
  board[3][3]=0;
  board[3][4]=0;
  board[3][5]=0;
  board[3][6]=0;
  board[3][7]=0;
  board[4][0]=0;
  board[4][1]=0;
  board[4][2]=0;
  board[4][3]=0;
  board[4][4]=0;
  board[4][5]=0;
  board[4][6]=0;
  board[4][7]=0;
  board[5][0]=0;
  board[5][1]=0;
  board[5][2]=0;
  board[5][3]=0;
  board[5][4]=0;
  board[5][5]=0;
  board[5][6]=0;
  board[5][7]=0;
  board[6][0]=1;
  board[6][1]=1;
  board[6][2]=1;
  board[6][3]=1;
  board[6][4]=1;
  board[6][5]=1;
  board[6][6]=1;
  board[6][7]=1;
  board[7][0]=2;
  board[7][1]=3;
  board[7][2]=4;
  board[7][3]=5;
  board[7][4]=5;
  board[7][5]=4;
  board[7][6]=3;
  board[7][7]=2;
}
char print_piece(board[x][y]);


int main()

{
 setup_board() ;  
char letter;
letter=print_piece(board[6][6]);
printf("%c\n");//to check an output
scanf("%d",&x);//to stop the compiler
return 0;
}

char print_piece(board[x][y])
{

     int choice;
     char piece;
     board[x][y]=choice;
     switch(choice)
     {
                   case 1:
                        piece='P';
                        break;

                   case 2:
                        piece='R';
                        break;
                   case 3:
                        piece='N';
                        break;
                   case 4:
                        piece='B';
                        break; 
                   case 5:
                        piece='Q';
                        break;
                   case 6:
                        piece='K';
                        break;
                  default:
                        piece=' ';
                        break;
                        }
                        return piece;

     }

The compiler(DEV C++)" can't use print_piece as a function".Why??
How do I use it? thanx!!!

All this really needs is a little reformatting and an understanding that you've already defined the board, just go to the position.
Also, since you had the constants alread defined, I used them and then changed them to #define to remove the memory consumption.

I did not validate the positions on the board (for white/black placement).

#include<stdio.h>
#define empty 0
#define pawn 1
#define rook 2
#define knight 3
#define bishop 4
#define queen 5
#define king 6 

int board[8][8];

void setup_board()
{
   /* Normal board setup */
   board[0][0]=rook;
   board[0][1]=knight;
   board[0][2]=bishop;
   board[0][3]=queen;
   board[0][4]=king;
   board[0][5]=bishop;
   board[0][6]=knight;
   board[0][7]=rook;
   board[1][0]=pawn;
   board[1][1]=pawn;
   board[1][2]=pawn;
   board[1][3]=pawn;
   board[1][4]=pawn;
   board[1][5]=pawn;
   board[1][6]=pawn;
   board[1][7]=pawn;
   board[2][0]=empty;
   board[2][1]=empty;
   board[2][2]=empty;
   board[2][3]=empty;
   board[2][4]=empty;
   board[2][5]=empty;
   board[2][6]=empty;
   board[2][7]=empty;
   board[3][0]=empty;
   board[3][1]=empty;
   board[3][2]=empty;
   board[3][3]=empty;
   board[3][4]=empty;
   board[3][5]=empty;
   board[3][6]=empty;
   board[3][7]=empty;
   board[4][0]=empty;
   board[4][1]=empty;
   board[4][2]=empty;
   board[4][3]=empty;
   board[4][4]=empty;
   board[4][5]=empty;
   board[4][6]=empty;
   board[4][7]=empty;
   board[5][0]=empty;
   board[5][1]=empty;
   board[5][2]=empty;
   board[5][3]=empty;
   board[5][4]=empty;
   board[5][5]=empty;
   board[5][6]=empty;
   board[5][7]=empty;
   board[6][0]=pawn;
   board[6][1]=pawn;
   board[6][2]=pawn;
   board[6][3]=pawn;
   board[6][4]=pawn;
   board[6][5]=pawn;
   board[6][6]=pawn;
   board[6][7]=pawn;
   board[7][0]=rook;
   board[7][1]=knight;
   board[7][2]=bishop;
   board[7][3]=king;
   board[7][4]=queen;
   board[7][5]=bishop;
   board[7][6]=knight;
   board[7][7]=rook;
}

char print_piece(int x, int y)
{
   char piece = ' ';
   switch(board[x][y])
   {
      case pawn:
         piece='P';
         break;
      case rook:
         piece='R';
         break;
      case knight:
         piece='N';
         break;
      case bishop:
         piece='B';
         break; 
      case queen:
         piece='Q';
         break;
      case king:
         piece='K';
         break;
      default:
         piece=' ';
         break;
   }

   return piece;
}

int main()
{
   int x=0;
   char letter=0;
   /**/
   setup_board(); 
   letter=print_piece(6,6);
   printf("%c\n", letter);//to check an output

   printf("Press number key to exit:");
   scanf("%d",&x);//to pause the program
   return 0;
}

Thanx works great!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.