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

pass dynamic 2d array to function c
pass two dimensional array to function c by reference
pass 2d array to function c++ without size
how to pass a 2d array to a function in c++
passing 2d dynamic array to function - c++
passing 2d array to function in c using pointers
how to pass 2d string array to function in c
passing 2d array to function in c++ using pointers

I am trying to implement a binary tree as a 2d array. I want the user to enter the required height of the tree and the program should give an appropriate size array. Then, I 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?

#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 << "*" << " ";
    }
}
string** tree [3][maxNumberOfNodes];

is the syntax of a static 2D array of type string** , where both dimensions have to be declared const.

The difference between a static and a dynamic array is shown in here: Multidimensional variable size array in C++

Instead you want to write something like

string** tree = new string*[3];
for(int i = 0; i < 3; i++)
   tree[i] = new string[maxNumberOfNodes];

As @Remy Lebeau commented: Every occurrence of new[] needs to be answered by a delete[] call, like this:

for (int i = 0; i < 3; i++)
    delete tree[i];
delete[] tree;

to remove the dynamic allocation from the heap.

Like @drescherjm pointed out sizeof(tree) is not valid, as tree is just a pointer and does not include size information about the array.

You could solve this by additionally passing the dimensions of your array with it:

void printTree (string** tree, int dim, int dim2)

and rewriting the loop to

for(int i = 0; i < dim; i++){
  for(int j = 0; j < dim2; j++){
    cout << tree[i][j]; //...
  }
}

How to pass a 2D array as a parameter in C?, for (int j = 0; j < n; j++) { arr[i][j] = i + j; In the previous post, we have discussed how to dynamically allocate memory for 2D array. In this post, we will see how we can pass 2D array to a function in C programming language. 1. For Static Array. If we know the array bounds at compile time, we can pass a static 2D array to a function in C as shown below.

string** tree [3][maxNumberOfNodes];

This declares a 2D array of string** pointers. That is not what you want. You want a 2D array of string objects instead, so drop the pointers:

string tree [3][maxNumberOfNodes];

Also, your printTree() is not implemented correctly. It would need to be implemented more like this instead:

void printTree(string** tree, int height) {
    for(int i = 0; i < 3;  i++) {
        for(int j = 0; j < height; j++) {
            // use tree[i][j] as needed ...
        }
    }
}

That being said, since the value of maxNumberOfNodes is not known until runtime, the string tree [3][maxNumberOfNodes] syntax is declaring a Variable Length Array, which is not officially supported by the C++ standard, only as an extension by a few C++ compilers. You need to use new[] instead to allocate the 2nd dimension:

string* tree [3];
for(int i = 0; i < 3; ++i)
    tree[i] = new string[maxNumberOfNodes];

printTree(tree, maxNumberOfNodes);

for(int i = 0; i < 3; ++i)
    delete[] tree[i];

Or better, use std::vector instead:

std::vector<string> tree [3];
for(int i = 0; i < 3; ++i)
    tree[i].resize(maxNumberOfNodes);

Though, in this latter case, you won't be able to pass tree to a string** function parameter, so you will have to adjust the code accordingly.

How to pass a dynamic 2d array of strings as a parameter in C++ , memory of size n for each row. This post is an extension of How to dynamically allocate a 2D array in C? A one dimensional array can be easily passed as a pointer, but syntax for passing a 2D array to a function can be difficult to remember. One important thing for passing multidimensional arrays is, first array dimension does not have to be specified.

the method call is given by

printTree(tree [3][maxNumberOfNodes]);

it's working for me

How to pass 2D array as a function parameter in C++, How to dynamically allocate a 2D array in C? A one dimensional array can be easily passed as a pointer, but syntax for passing a 2D array to� Passing 2d array to function in c using pointers. The first element of the multi-dimensional array is another array, so here, when we will pass a 2D array then it would be split into a pointer to the array. For example, If int aiData[3][3], is a 2D array of integers, it would be split into a pointer to the array of 3 integers (int (*)[3]).

How to pass a 2D array as a parameter in C, string** tree [3][maxNumberOfNodes];. is the syntax of a static 2D array of type string** , where both dimensions have to be declared const. In this post, we will see how to pass 2D array as a function parameter in C++ programming language. If we know the array dimensions at compile time, we can pass the array by reference using a function template in C++. The advantage of using this approach is that there is no need to specify the array dimensions.

Passing Dynamically Allocated Two dimensional Array to a Function , The disadvantage of using this approach is that we can't use it with dynamic arrays. We can also create a wrapper over the function as shown below which is more� There are two ways to pass dynamic 2D array to a function: 1) Passing array as pointer to pointer (int **arr) Using new operator we can dynamically allocate memory at runtime for the array. New operator returns the address of the space allocated.This method Passes array reference as double pointer to the function along with rows and columns.

How to pass an array as a parameter in C?, One important thing for passing multidimensional arrays is, first array dimension does not have to be specified. The second (and any subsequent) dimensions must� Similarly, you can pass multi-dimensional arrays as formal parameters. Way-1. Formal parameters as a pointer − void myFunction(int *param) { . . . } Way-2. Formal parameters as a sized array − void myFunction(int param[10]) { . . . } Way-3. Formal parameters as an unsized array − void myFunction(int param[]) { . . . } Example

Comments
  • I assume you are not permitted to use std::vector?
  • There are no dynamic arrays in c++. You are using variable length arrays, and it's a compiler extension. It would be better to use a std::vector<std::vector<std::string>> instead.
  • for(int i=0; i < sizeof(tree); i++){ Note that sizeof() will not work on a pointer. remember sizeof() is a compile time constant.
  • @drescherjm your assumption is correct
  • Don't forget to delete[] the arrays when you are done with them: for(int i = 0; i < 3; i++) { delete[] tree[i]; } delete[] tree;
  • using void printTree(string** tree, int height) throws an error for me. I get the error: "cannot convert ‘int**’ to ‘int***’ for argument ‘1’ to ‘void printTree(int***, int)’ " however, printTree(string* tree, int height) works as expected
  • @uhexos a tree of string values works fine for me. If you are having errors with a separate tree of int values, then you are doing something wrong with it. Please edit your question to include that code. Looks like you need to go back to basics and review how pointers work in general.
  • I'm trying to implement it as printTree(tree)