Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

closed How to Pass 2D Dynamic Char Array Into a Function And Initialize.

0 votes
asked Apr 1 by Dakota Bell (200 points)
closed Apr 12 by Dakota Bell

I am attempting to pass a declared 2D dynamic character array into a function, and to then initialize the dynamic array within the function. However, it seems to be having issues determining the memory where this initialization should occur. 

When I perform the initialization in the same body where I declare the array, the code functions perfectly. Hence why I am assuming there is some kind of memory-reference issue.

I do not have access to my personal debugger, and couldn't determine anything using the debugger provided by OnlineGDB. Any assistance would be appreciated.

int main() { // Executes main procedures of the program.
    const int ROWS = 5;
    const int COLS = 5;
    char **arr;
    
    initializeArr(arr, ROWS, COLS);
    
    displayArr(arr, ROWS, COLS);
    
    return 0;
}

void initializeArr(char **arr, const int rows, const int cols)    {
    arr = new char* [rows];
    // Declare 2nd dimension
    for (int r = 0; r < rows; r++)  {
        arr[r] = new char[cols];
    }
    // Define each element in the array.
    for (int r = 0; r < rows; r++)  {
        for (int c = 0; c < cols; c++)  {
            arr[r][c] = 'T';
        }
    }
}
void displayArr(char **arr, const int rows, const int cols)   {
    for (int r = 0; r < rows; r++)  {
        for (int c = 0; c < cols; c++)  {
            std::cout << std::setw(2) << std::right << arr[r][c] << std::flush;
        }
        
        std::cout << std::endl;
    }
}
closed with the note: Answer provided by Peter Minarik. Thank you Peter!

1 Answer

+1 vote
answered Apr 8 by Peter Minarik (55,760 points)
selected Apr 12 by Dakota Bell
 
Best answer

You have a 2-dimensional array that you pass to your function as char ** arr. That is, you pass the address to your initializeArr() function, where your array is located in the memory.

If you want to create a new array, you will do it in a memory address returned to you by new. While you can change the memory address where arr points to, it only changes the value (i.e., the memory address) of arr  within your initializeArr() function as when you called this function the value (of the memory address) is copied to arr.

What you need is one more layer of reference/dereference:

void initializeArr(char ***arr, const int rows, const int cols)
{
    *arr = new char* [rows];
    // Declare 2nd dimension
    for (int r = 0; r < rows; r++)
    {
        (*arr)[r] = new char[cols];
    }
    // Define each element in the array.
    for (int r = 0; r < rows; r++)
    {
        for (int c = 0; c < cols; c++)
        {
            (*arr)[r][c] = 'T';
        }
    }
}
// ...
int main()
{
    // ...
    initializeArr(&arr, ROWS, COLS);
    // ...
}

I hope this helps.

commented Apr 12 by Dakota Bell (200 points)
Thank you for the assistance Peter Minarik! I knew it was some issue with referencing, however I was unable to connect the dots.
Of course, the answer seems blindly obvious now that I've seen the solution. Thank you again!
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...