tags:

views:

177

answers:

1

I have:

int main(int argc, char **argv) {
   if (argc != 2) {
      printf("Mode of Use: ./copy ex1\n");
      return -1;
   }

   formatDisk(argv);
}

void formatDisk(char **argv) {
   if (argv[1].equals("ex1")) {
       printf("I will format now \n");
   }
}

How can I check if argv is equal to "ex1" in C? Is there already a function for that? Thanks

+11  A: 
#include <string.h>
if(!strcmp(argv[1], "ex1")) {
    ...
}
Dave
Should you also be checking for null or ensure that this index exists first?
Chris Ballance
argc gives the count of arguments in argv, so the check of (if argc != 2) assures that argv[1] exists.
McWafflestix
Also worth noting the strncmp() function, which compares the first 'n' bytes of the strings. (http://www.manpagez.com/man/3/strncmp/)
rascher