This very simple code:
#include <iostream>
using namespace std;
void exec(char* option)
{
cout << "option is " << option << endl;
if (option == "foo")
cout << "option foo";
else if (option == "bar")
cout << "opzion bar";
else
cout << "???";
cout << endl;
}
int main()
{
char opt[] = "foo";
exec(opt);
return 0;
}
generates two warnings: comparison with a string literal leads to unspecified behavior.
Can you explain why this particular code is not working, but if I change
char opt[]
to
char *opt
Does it work, but generates a warning? Is this related to the termination \ 0? What is the difference between two opt ads? What if i use const constructor? The solution is to use std :: string?
source
share