Is it considered bad practice to pass string literals as char*, when an array and not a c-string is expected by the function? For example, this will compileFrom my code:
voidchar a_funcConsoleIO::GetNextChar(string prompt, char* letterschoices, int numLettersnumChoices) {
// Loop until valid input, or 10 times.
for (int i = 0; i < numLetters;10; i++) {
coutWrite(prompt);
<< letters[i] << endl;
}char* buffer = new char[200];
} cin.getline(buffer, 200);
int main char choice = tolower(buffer[0]);
if (in(choice, choices, numChoices)) {
a_func return choice;
}
WriteLine("This"Invalid workschoice.");
}
// Just return the first choice as the default.
return choices[0];
}
// In main:
char userChoice = ConsoleIO::GetNextChar("Make a selection: ", "234", 3);
ButIt compiles, but is it considered bad practice?
Is it considered bad practice to pass string literals as char*, when an array and not a c-string is expected by the function? For example, this will compile:
void a_func(char* letters, int numLetters) {
for (int i = 0; i < numLetters; i++) {
cout << letters[i] << endl;
}
}
int main()
{
a_func("This works.");
}
But is it considered bad practice?
Is it considered bad practice to pass string literals as char*, when an array and not a c-string is expected by the function? From my code:
char ConsoleIO::GetNextChar(string prompt, char* choices, int numChoices) {
// Loop until valid input, or 10 times.
for (int i = 0; i < 10; i++) {
Write(prompt);
char* buffer = new char[200];
cin.getline(buffer, 200);
char choice = tolower(buffer[0]);
if (in(choice, choices, numChoices)) {
return choice;
}
WriteLine("Invalid choice.");
}
// Just return the first choice as the default.
return choices[0];
}
// In main:
char userChoice = ConsoleIO::GetNextChar("Make a selection: ", "234", 3);
It compiles, but is it bad practice?
String literals as char arrays
Is it considered bad practice to pass string literals as char*, when an array and not a c-string is expected by the function? For example, this will compile:
void a_func(char* letters, int numLetters) {
for (int i = 0; i < numLetters; i++) {
cout << letters[i] << endl;
}
}
int main()
{
a_func("This works.");
}
But is it considered bad practice?