I am trying to compare the parameter of command with argv[] but it’s not working. Here is my code.
./a.out -d 1
In main function
int main (int argc, char * const argv[]) {
if (argv[1] == "-d")
// call some function here
}
But this is not working… I don’t know why this comparison is not working.
You can’t compare strings using
==. Instead, usestrcmp.The reason for this is that the value of
"..."is a pointer representing the location of the first character in the string, with the rest of the characters after it. When you specify"-d"in your code, it makes a whole new string in memory. Since the location of the new string andargv[1]aren’t the same,==will return0.