2D array as parameter

Hi,

I've been trying to solve this, but i just can't seem to figure it out. Been searching all over the web.

How come this code works fine, if i put 4 into the my 2D array like this:
char board[boardHeight][4];

but if i put in a variable equal to four, it will not build?
char board[boardHeight][boardWidth];

Help explaining this is very much appreciated :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <vector>

using namespace std;

int boardWidth = 4;
int boardHeight = 4;

void drawBoard(int h, int w, char b[][4]);

int main(int argc, const char * argv[]) {
    
    char board[boardHeight][4];
    
    vector<int> playerPos (2,2);
    
    for(int i = 0; i < boardHeight; i++){
        for(int j = 0; j < boardWidth; j++){
            board[i][j] = '.';
            cout << board[i][j];
        }
        cout << "\n";
    }
    
   drawBoard(boardHeight,boardWidth, board);
    
    return 0;
}

void drawBoard(int h, int w, char b[][4]){
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            cout << b[i][j];
        }
        cout << "\n";
    }
}
Hi,

Make the variables const. Array sizes must be a compile time constant :+)
Oh, of course! Thank you sir! :)
Oh, and make the parameters to your function const as well, it's a promise that you won't be changing them inside the function :+)

The variables could be unsigned as well, the normal type for the size of things is std::size_t which is the largest unsigned type the system has. Either that or unsigned short int, seen as the board is small. In terms of speed, std::size_t is probably faster, because it quicker to retrieve a machine word (64 or 32 bit) than something smaller. So what I have done is use the type system to enforce the idea that it is a positive value.

Don't have using namespace std; put std:: before each std thing, it's the best way and is what all the experts do, Google it to understand why:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <vector>

// using namespace std;



void drawBoard(const std::size_t boardHeight, // alternative layout for multiple parameters
                       const std::size_t boardWidth, // name the parameters the same as arguments
                       char b[][boardWidth]);            // it reinforces the idea they are the same

int main(/*int argc, const char * argv[]*/) { // comment these to avoid warning about unused parameters
                                                            // or don't have them if you don't use them

   // put these consts here, so they aren't global, you send them as arguments anyway
   const std::size_t boardWidth = 4;
   const std::size_t boardHeight = 4;
    
    char board[boardHeight][boardWidth];
    
    // std::vector<int> playerPos (2,2); // what does this do? Unused variable uncomment it when you need
                                                      // to use in future
    
// this part should be a function InitialiseBoard
    for(int Row = 0; Row < boardHeight; ++Row){ // pre increment doesn't copy, so is more efficient
        for(std::size_t Col = 0; Col < boardWidth; ++Col){ //use Row and Col (that is what they are), not i and j
            board[Row][Col] = '.';                                      // i and j can be error prone
             // std::cout << board[Row][Col]; // don't really need to print here, use drawBoard 
        }
         // std::cout << "\n";
    }
    
   drawBoard(boardHeight,boardWidth, board);
    
    return 0;
}

// does this display the rows correctly?
void drawBoard(const std::size_t boardHeight, 
                       const std::size_t boardWidth, 
                       char b[][boardWidth])
{
   for(std::size_t Row = 0; Row < boardHeight; ++Row){ 
        for(std::size_t Col = 0; Col < boardWidth; ++Col){{
            std::cout <<  board[Row][Col];
        }
         std::cout << "\n";
    }
}


I haven't checked any of this, hopefully not too many copy/paste errors :+)
Last edited on
Topic archived. No new replies allowed.