1

void build() will create a 2D array whose size is determined at run time and pass it to modify() by reference.

void modify() will make some change of the array.

.h file:

void modify(______, int mySize);
void build(int size);

in .cpp file:

void modify(______, int mySize) {
    for (int i = 0; i < mySize; i++) 
        for (int j = 0; j < mySize; j++)
            myArray[i][j] = false;
}

void build(int size) {
    bool myArray[size][size];
    modify(______, size);
}

Can someone tell me what to put in these blanks? I tried many way to cast myArray but still not working. Thank you so much!

6
  • See stackoverflow.com/questions/6321670/… Commented Mar 10, 2013 at 10:58
  • Write a class that represents a 2D array, using an single std::vector vector to hold the data. Commented Mar 10, 2013 at 11:04
  • Jack, thanks for the link. But my size is not known until build()is called. The compiler says ".h: error: 'size' was not declared in this scope". Commented Mar 10, 2013 at 11:04
  • 2
    Variable length arrays are non-standard. bool myArray[size][size]; uses a compiler extension. Commented Mar 10, 2013 at 11:13
  • Can you be more specific? Commented Mar 10, 2013 at 11:20

2 Answers 2

1

First, note that variable length arrays (VLAs) are not standard C++. The fact that this line compiles is due to a GCC compiler extension:

bool myArray[size][size];

It simply isn't valid C++. The dimensions of your array need to be compile time constants, yet here you're using arguments passed to your function build.

Anyway, to pass a VLA to a function, you have to continue using compiler extensions. However, as far as I've tested, these only work when compiling as C. The first option uses an extension that allows parameters to be used in other parameter declarations:

void modify(int mySize, bool array[][mySize]);

The second option, if you want mySize to be the second argument, also uses a compiler extension allowing forward declarations of parameters:

void modify(int mySize; bool array[][mySize], int mySize);

Here int mySize; is a parameter forward declaration.

Nonetheless, you really shouldn't be using variable length arrays. You can dynamically allocate arrays of variable length, like so:

void modify(bool** array, int mySize);

void build(int size) {
  bool** myArray = new bool*[size];
  for (int i = 0; i < size; i++) {
    myArray[i] = new bool[size];
  }
  modify(myArray, size);
}

However, this is still not a recommended practice in C++. You should avoid dynamic allocation unless you really need it, and when you do need it you should manage it in some way. Here, you would need to remember to delete[] each element of myArray and myArray itself.

What you should really be using is the standard containers. A std::vector<std::vector<bool>> would suit you well here:

void modify(std::vector<std::vector<bool>>& array);

void build(int size) {
  std::vector<std::vector<bool>> myArray(size, std::vector<bool>(size));
  modify(myArray);
}

Now you don't even have to pass along the size.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much! btw void modify(std::vector<std::vector<bool>> & array); would make it pass by reference
@user2153561 Of course! I totally didn't realise that the modify function was going to modify its argument.
0

Use it this way:

    void modify(bool**& myArray, const int mySize) 
    {
        for (int i = 0; i < mySize; i++) 
            for (int j = 0; j < mySize; j++)
               myArray[i][j] = false;
    }

    void build(const int size) 
    {
        // create the array
        bool** myArray = new bool*[size];
        for (int i=0; i<size; i++)
            myArray[i] = new bool[size];

        modify(myArray, size);

        // release the array
        for (int i=0; i<size; i++)
            delete[] myArray[i];
        delete[] myArray; 
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.