0

How to pass the 2d array to function I have created one and wanted to define function which initialize this array.

#include <iostream>

using namespace std;
int n = 5;**strong text**
void wypelnijTabliceBooli(bool** b){
      for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++)
            b[i][j] = 0;
    }
}
int main(){
    bool b[n][n];
    wypelnijTabliceBooli(b);
    return 0;
}

and i get

error: cannot convert 'bool (*)[(((unsigned int)(((int)n) + -0x000000001)) + 1)][(((unsigned int)(((int)n) + -0x000000001)) + 1)]' to 'bool**' for argument '1' to 'void wypelnijTabliceBooli(bool**)'|
Yoda
  • 17,363
  • 67
  • 204
  • 344

4 Answers4

1

Arrays aren't pointers. Only the "first" (dominant) dimension of an array can decay into a pointer when passed to a function. Otherwise, if you have a multidimensional array, you have to declare it in the function argument list accordingly:

void funcTaking2DArray(int (*arr)[5])
{
    // do stuff
}

int array[10][5];
funcTaking2DArray(array);
  • @WhozCraig Thanks, didn't know about this terminology. Anyways, that's "first", and not "last". –  Apr 06 '13 at 08:16
  • +1 nice answer =) yeah, its the dimension with all the gusto, as it is the magnitude order for all others that follow *combined*. – WhozCraig Apr 06 '13 at 08:16
1

Use dimensions:

void wypelnijTabliceBooli(bool b[n][n])

You could pass it as a pointer to array, but all but the "outermost" dimension needs to be known:

void wypelnijTabliceBooli(bool (* b)[n])

Edit: I accept that this, with everything else unmodified, requires the GNU or other compilers that have extensions above C++ standard. The alternative is to make n a const value.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • Actually, all outer dimensions need to be known, only the first one can be omitted. –  Apr 06 '13 at 08:14
  • I wonder if this is another gcc extension, or C99? – john Apr 06 '13 at 08:16
  • @john, GCC has VLAs, yes, but not wit good compiler options turned on. – chris Apr 06 '13 at 08:16
  • @chris no kidding? I thought that feature was *never* going to be in C++. Why would they put it in? How odd. – WhozCraig Apr 06 '13 at 08:19
  • @WhozCraig, There's a proposal for it, actually. – chris Apr 06 '13 at 08:21
  • @chris I swear one way or another `_alloca()` will continue to haunt us. You're pretty young so you may not have had the pleasure of that little fiasco. – WhozCraig Apr 06 '13 at 08:22
  • @WhozCraig, I can't say I have, but I'll enjoy reading about it. The oldest thing I've had to deal with directly was figuring [this one](http://stackoverflow.com/questions/11622115/still-the-error-expected-asm-or-attribute-before-to) out. – chris Apr 06 '13 at 08:30
  • @chris thats *awesome*. 20-bit linear addresses formed via 16-bit selector << 4 | 16-bit address. Good times. By the time 286-protected mode came along it was utter salvation for us. – WhozCraig Apr 06 '13 at 08:31
  • @WhozCraig, Well, I guess I have learned a bit from my wanderings around Windows, including 16-bit Windows. It's hard not to stumble upon things when they typedef `char *` as `LPSTR` (I'm sure you can guess what it stands for), but there are a lot of things that work so differently from now. Raymond Chen does a good job of that when it's related to Windows. – chris Apr 06 '13 at 08:34
  • @chris I know he's done it a long time. I like reading his old new thing blog regularly. Just imagine back in the day when your DLL was *the* only copy in all of system RAM, the application concurrency was mind-bending. Like I said. good times. (and Long Pointer to String =P, not a guess, used at least a million times in my career) – WhozCraig Apr 06 '13 at 08:36
0

It seems like you want the size of the array to be flexible. You can declare b as:

bool** b = new bool*[n]
for (i=0; i<n; i++)
    b[i] = new bool[n]
dan
  • 982
  • 8
  • 24
0

As long as I know when you use:

void wypelnijTabliceBooli(bool** b){
  for(int i = 0; i < n; i++){
    for(int j = 0; j < n; j++)
        b[i][j] = 0;
}
}

you can't use indexes do address the bool two dimensional array. This happens because bi-dimensional arrays are stored in contiguous memory places and if you don't provide at least one of the array dimensions there is no way of knowing when does knowing when a given "line ends". example: Imagine you have a bool array:

bool myArray[2][3]={{true,true,false},{false,false,true}};

In memory this will be like that (where [] represents a memory block):

[true][true][false][false][false][true]

and by passing only a pointer to it now you have no way of knowing if this was array[3][2] or array[2][3].

This is solved by passing to the function all but one dimension of the array, like this:

void wypelnijTabliceBool(bool b[][3] ){
  for(int i = 0; i < n; i++){
    for(int j = 0; j < n; j++)
        b[i][j] = 0;
}
}

(if your second dimension was 3).

Mppl
  • 941
  • 10
  • 18