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;
}
}