c - Comparing char* with char -
i working on program name of file input in command line. need check if input given character "-" , work file according result, not quite sure how it. method have tried, seemed logically make sense, of checking if
argv[1] == "-";
always returns zero, when write "-" in command input. can do?
you need write strcmp(argv[1],"a")==0;
in example, compared 2 pointers , not 2 strings.
when compiling, compiler declares "a" somewhere in memory , subsitute in compile time memory address. since argv[1] can't sit on same byte (temporary) "a" , result false. need iterate on 2 strings , iterativly compare each character. strcmp
compares 2 strings , return 0 if equale in exact manner.
for more info on strcmp
: http://www.cplusplus.com/reference/cstring/strcmp/
in order handle multiple characters, can place few if-else's :
if (strcmp(argv[1],"-")==0){ minus_character_handling_function(); } else if (strcmp(argv[1],"+")==0){ plus_character_handling_function(); } else if (strcmp(argv[1],"a")==0){ a_character_handling_function(); }
Comments
Post a Comment