I am having understanding a weird behavior of strcmp
function, which will be illustrated by the following code:
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char *p = "no";
cout << p << endl; //Output: no
cout << &p << endl; //Output: 0x28ac64
cout << strlen(p) << endl; //Output: 2
cout << strcmp(p, "no") << endl; //Output: 0
cin >> p; //Input: bo
cout << p << endl; //Output: bo
cout << &p << endl; //Output: 0x28ac64
cout << strlen(p) << endl; //Output: 2
cout << strcmp(p, "no") << endl; //Output: 0
return 0;
}
What I fail to understand is why the output of line 15 is 0. 0 means the two strings are equal, which is clearly not the case. What am I missing here?
P.S. I do apologize for the escape characters in the headers, but I could not get iostream to display if I removed it. Though I am posting this, I will figure out how to get it right for next time. :)