I am developing a tic-tac-toe board game in c++ for learning purposes, and I am passing a char**
to a function that assigns the given parameter an array of pointers which stores a players name.
Upon variable assignment, I am able to print the results of the initialized variable in the function call to void initPlayerNames(char** playerNames)
but when I return to the caller function the program does not print the results.
My understanding is that a pointer variable stores the address of another data structure, therefore I expect to print the results of the pointer variable after it has been modified in the function call.
Please help me understand where I am making a mistake
void initPlayerNames(char** playerNames) {
const int playerNameLength = 100;
cout << "Player one, enter your name: ";
char p1[playerNameLength];
cin.getline(p1, playerNameLength);
cout << "Player two, enter your name: ";
char p2[playerNameLength];
cin.getline(p2, playerNameLength);
char* names[2] = {p1, p2};
playerNames = names;
cout << "Player 1 = " << playerNames[0] << endl;
cout << "Player 2 = " << playerNames[1] << endl;
};
void game() {
char** playerNames = nullptr;
cout << "Welcome! \n";
initPlayerNames(playerNames);
cout << "Player 1 = " << playerNames[0] << endl;
cout << "Player 2 = " << playerNames[1] << endl;
};
Output
Player one, enter your name: Kevin Patel
Player two, enter your name: Steve Jobs
Player 1 = Kevin Patel
Player 2 = Steve Jobs