0

I'm learning C++ for my test where I learn java before, and now there's a lot of confusion. I'm trying to print 2D array of boolean, returning # if true, and ? if false. I've tried initializing the array first, which I think works, and I can print the value in certain array location if I don't use the iterator (printGrid). below is my code

 #include<iostream>

 void printGrid(bool a[][10]){ // why this method doesn't work?

    for(int i = 0; i < sizeof(a)/ (sizeof(*a)) ; i++){
        for(int j = 0; j < sizeof(a)/ (sizeof(*a)) ; j++) {
            char x = a[i][j] ? '#' : '?'; // i've using the conditional in my std::cout before, still doesn't work
            std::cout << x;

        }
        std::cout<< std::endl;
    }

   }
void main(){
    bool grid[10][10];
        for(int i = 0; i < sizeof(grid)/sizeof(*grid) ; i++){
        for(int j = 0; j < sizeof(grid)/sizeof(*grid); j++){

            grid[i][j] = 1;
        }

    }
    printGrid(grid); // this doesn't work
    std::cout<<( (grid[0][1]) ? "#" : "?" ) << std::endl; // this works fine
    getchar();

} // main

where do I got my printGrid wrong? i've tried using a 2D array of INT instead with the same setup, with function outside main etc, and it worked just fine. any idea?

edit: thanks a lot for the answers. my question hasn't been answered though, what is wrong with my function? because i've tried using a method with the same setup, just different parameter, and it worked fine

void printint(int a[][10]){ // why does this work, and the above doesn't?
for(int i = 0; i < 10 ; i++){
        for(int j = 0; j < 10 ; j++) {

            std::cout <<( (a[i][j] == 10) ? "a" : "b");

        }
        std::cout << std::endl;
    }
}

when I use printint to print my 2D array of int, it worked just fine?

Rei
  • 492
  • 6
  • 18

2 Answers2

2

bool a[][10] is in fact bool (*a)[10], so sizeof (a) is the size of a pointer.

Pass array by reference:

void printGrid(const bool (&a)[10][10])

Live example

Using std::array<std::array<bool, 10u>, 10u> has a more intuitive interface.

Jarod42
  • 173,454
  • 13
  • 146
  • 250
  • I have errors saying array of references are illegal with that code. I've tried writing the same method with int a [][10] parameter btw, and it worked fine. only in my printGrid method that has boolean parameter doesn't. – Rei Oct 28 '14 at 15:49
  • @Rei: I provided a link with working code. I suspect that you miss the parenthesis for the reference to array. – Jarod42 Oct 28 '14 at 15:51
  • @Rei what compiler are you using? It works fine for me too. (http://ideone.com/WN6LfU independant from Jarod's) – Baldrickk Oct 28 '14 at 15:55
  • @Jarod42 hi, sorry, the code worked, thanks. it doesn't answer my question though, would you take a lot at my edited post? I've added the printint method I used to print 2D array, and I didn't use the (&a)[10][10] and it worked. why? – Rei Oct 28 '14 at 15:59
  • @Rei: I answered that too. `sizeof(a)` is not the size of the array in your case, but the size of a pointer (mainly 4 (32bits) or 8 (64bits)). – Jarod42 Oct 28 '14 at 16:03
0

Here you are passing array to a function which is converted to pointer so all you will get is the size of the pointer.

Remember arrays decay to pointers when the passing format requests them to decay, i.e. if the corresponding parameter is declared as a pointer (as in this case). If the parameter were declared as a reference to an array, the decay wouldn't take place

ravi
  • 10,474
  • 1
  • 12
  • 30