0

so to summarize this question,my goal is to print "works!\n". Whenever I manually give the char pointer action variable the word "exit" (the comment line) , I get my output.Whenever I don't (and instead give it through the command line arguments when I run a.out) I dont get it (which essentially means that the "if" condition fails). Any ideas why this happens?

int main(int argc,char *argv[]){
    char *action;
    action = argv[1];
    /*action = "exit";*/
    printf ("%s\n",action);
    if (action == "exit"){
        printf ("works!\n");
    }
}
  • 1
    In place of this `if (action == "exit"){`, do this `if (!strcmp (action, "exit")){` and it will work in both the case. – H.S. Dec 28 '21 at 12:43
  • ... and don't forget to check that `argc >= 2` to make sure that `argv[1]` is actually valid before you access it. – Ted Lyngmo Dec 28 '21 at 12:45

0 Answers0