Thread: Passing 2d Array to function?

  1. #1
    Marcosm64
    Guest

    Passing 2d Array to function?

    I am triying to pass a 2d Array to a function, but I dont know how, can someone help? here is my code:
    Code:
    #include <iostream>
    using namespace std;
    
    void PrintBoard(int* TheBoard, int Rows, int Columns); 
    
    int main()
    {
    	int Board[5][5];
    	int i = 0, j = 0;
    
    	for (i = 0; i < 5; i++)
    	{
    		for (j = 0; j < 5; j++)
    			Board[i][j] = '\0';
    	}
    
    	Board[2][2] = 5;
    
    	PrintBoard(Board, 5, 5);
    
    	return 0;
    }
    
    void PrintBoard(int* TheBoard, int Rows, int Columns)
    {
    	int i = 0, j = 0;
    
    	for (i = 0; i < Rows; i++)
    	{
    		for (j = 0; j < Columns; j++)
    			cout << TheBoard[i][j];
    		cout << endl;
    	}
    }
    Thanks in advance.

  2. #2
    The Pantless Man CheesyMoo's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Code:
    void PrintBoard(int board[][],int row,int collum)
    {
    bla
    bla
    bla
    }
    Would that work for ya?
    If you ever need a hug, just ask.

  3. #3
    cereal killer dP munky's Avatar
    Join Date
    Nov 2002
    Posts
    655
    like salem said..that wont work, it shouldnt even compile if memory serves

    but this should...
    Code:
    void PrintBoard(int board[][5],int row,int collum)
    {
        /* Code */    
    }
    as long as you define the number of colums so the compiler knows whats going on.....otherwise the compiler doesnt know how to map out your array
    guns dont kill people, abortion clinics kill people.

  4. #4
    The Pantless Man CheesyMoo's Avatar
    Join Date
    Jan 2003
    Posts
    262
    I didn't know that.

  5. #5
    cereal killer dP munky's Avatar
    Join Date
    Nov 2002
    Posts
    655
    i didnt either until i got it wrong on a test, and i was trying to explain to the teacher why i put what i put, and he said all that needs to be defined is the colums...simply for the reason, if you had a 1 dimensional array....thats all you have, columns
    guns dont kill people, abortion clinics kill people.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 10-21-2007, 07:44 AM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. passing 2D array to a function
    By ashesh in forum C Programming
    Replies: 4
    Last Post: 06-09-2003, 11:16 PM
  5. passing an array to a function (2d array)
    By revelation437 in forum C Programming
    Replies: 5
    Last Post: 03-11-2003, 03:32 PM