How to pass a dynamic 2d array as a parameter in C++

I am trying to implement a binary tree as a 2d array. Now I want to the user to enter the required height of the tree and the program gives an appropriate size array. I then want to print the array which is why I need to pass it as a parameter. However, I get the following error

arrayTree/main.cpp|19|error: cannot convert ‘std::__cxx11::string** (*)[maxNumberOfNodes] {aka std::__cxx11::basic_string<char>** (*)[maxNumberOfNodes]}’ to ‘std::__cxx11::string** {aka std::__cxx11::basic_string<char>**}’ for argument ‘1’ to ‘void printTree(std::__cxx11::string**)’|

Please what is causing the error and how can I fix it.

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
#include <iostream>
#include <string>
#include <math.h>

using namespace std;
void printTree(string** tree);
int main()
{
    int treeHeight = 0;
    int maxNumberOfNodes = 1;
    cout << "enter tree height";
    cin >> treeHeight;
    cout << treeHeight<< "\n";

    //create an array that can hold every combination for a given tree height
    maxNumberOfNodes = pow(2,treeHeight) - 1;
    string** tree [3][maxNumberOfNodes];
    cout << maxNumberOfNodes;
    printTree(tree);

}

 void printTree(string** tree){
//not fully implemented yet
    for(int i=0; i < sizeof(tree);  i++){
        cout << "*" << " ";
    }
}
You cannot create a static array with dynamic size. You have to allocate the memory for the array dynamically.

Consider using a 1D array. Read: http://www.cplusplus.com/articles/G8hv0pDG/

You have to pass the number of elements, because a pointer does not have that metadata.

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
#include <iostream>
#include <string>

using std::string;
using std::cout;

void printTree( const string* tree, size_t Rows, size_t Cols );

int main()
{
    int treeHeight = 0;
    int maxNumberOfNodes = 1;
    std::cin >> treeHeight;
    int maxNumberOfNodes = treeHeight*treeHeight - 1;
    cout << maxNumberOfNodes;
    if ( maxNumberOfNodes < 1 ) return 1;

    string* tree = new string [ 3*maxNumberOfNodes ];
    printTree( tree, 3, maxNumberOfNodes );

    delete [] tree; // remember to clean up
}

void printTree( const string* tree, size_t Rows, size_t Cols ) {
    for ( size_t row=0; row < Rows; ++row ) {
        cout << "*" << " ";
    }
}



EDIT:
Routine disclaimer: "Don't use new/delete in modern C++."

That is not quite true though. There are two cases, where explicit new in code is fine:

A. Teacher requires that in the homework.

B. When two stellar constellations do align. (A rare event.) The constellations are:
1. The clearest, most maintainable implementation is with new rather than modern C++ constructs
2. You truly master C++
Last edited on
Heh heh heh...

Gimme a sec and I’ll find an old response to this that will answer all your questions and more.


[edit]
Ok, so, I read the title of your post and not the problem...

The problem is you are doing something weird.
On line 17, you have defined an object that could collapse to the type: (string***)[3].

Don’t do that.

If you need an array of strings, specify the type correctly:

string tree[3][maxNumberOfNodes];

I am uncertain what you mean by “tree”. Are you doing something with actual trees? Or is this supposed to be some sort of graph?


As for messing with multidimensional array arguments, here is an answer I wrote some time ago exemplifying 2D arrays:
http://www.cplusplus.com/forum/general/49079/#msg266703
Last edited on
closed account (E0p9LyTq)
How to pass a dynamic 2d array as a parameter

I know I am going to hear howls of "but I can't use them!" but I still will offer up some advice:

Dynamic array: std::vector<>. Easy to use in a 2D arrangement, easy to pass as a parameter.

Properly templated you can create a function that can accept a 2D vector of any data type:

1
2
template<typename T>
void someFunction(const std::vector<std::vector<T>>&);

Oh wait, you are only using a 2D array because you want an array of C-style strings.

std::string as the data type in a 1D vector array. REALLY easy to pass:

void someFunction(const std::vector<std::string>&);

Want to modify the array in the function? Don't specify const.
Topic archived. No new replies allowed.