-3

I am working on a console project and I just can't get this part to work.

void execute(char* argv[]) {
    char* printex = "print";
    if (argv[1] == printex) {
        print(argv);
    }
    else {
        cout << "Unknown function." << endl;
    }
}

Every time I type in "print" for argv[1], it thinks I have typed in something else. I tried putting in

cout << argv[1];

and the output was print. Why is it not working then?

Ilya
  • 4,583
  • 4
  • 26
  • 51
Forrest4096
  • 159
  • 1
  • 8

2 Answers2

1

argv[1] is a char*, so is printex. Comparing them will compare the address they contain, not the actual string. So they will never be same. You can use std::string (which is safer), or in the current form, use strcmp for comparison.

if( strcmp( argv[1], printex) == 0 )
 //mathced
Rakib
  • 7,435
  • 7
  • 29
  • 45
1

Try to replace if (argv[1] == printex) with if (strcmp(argv[1], printex)==0) to compare strings (not pointers on strings).

Ilya
  • 4,583
  • 4
  • 26
  • 51