Help comparing an argv string
Asked Answered
S

3

7

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

Suzy answered 29/4, 2009 at 19:2 Comment(0)
A
24
#include <string.h>
if(!strcmp(argv[1], "ex1")) {
    ...
}
Ammo answered 29/4, 2009 at 19:4 Comment(3)
Should you also be checking for null or ensure that this index exists first?Archives
argc gives the count of arguments in argv, so the check of (if argc != 2) assures that argv[1] exists.Eba
Also worth noting the strncmp() function, which compares the first 'n' bytes of the strings. (manpagez.com/man/3/strncmp)Asocial
L
2

Just to give and example of using strings and dynamically allocating new strings. Probably useful when you don't know the size of argv[?]

// Make the string with the value you want compared
char testString[] = "-command";

// Make a char pointer, use new to allocate the memory 
//  the size is determined by string length of argv[1]
char * strToTest = new char[ strlen( argv[1] ) ];

// Now we can copy the contents of argv[1] into strToTest as they are equal size
strcpy( strToTest, argv[1] );

// Now strcmp returns True if the two strings match
if (strcmp( testString, strToTest ) {
//do somthing here ...
}
Libava answered 12/8, 2011 at 6:56 Comment(4)
why do we need a copy of argv[1]? strcmp takes const char *Mogador
@Raja; The copy isn't necessary. It purely for the sake of cramming extra string function examples in to the answer. Because if you're looking this question up, you probably want to see those too : ) ... as stated in the first two sentences of the answer.Libava
new is not very c-ish :> (answering a post 12 years old)Schoolmaster
The comment in the code of the answer "// Now strcmp returns True if the two strings match" is incorrect. The function strcmp will return zero (false) if the strings two strings match, not non-zero (true).Heaviside
S
0

Hello I just wanted to add that my functions arent correctly checking for the strcmp unless I check that they are == 0. Running on Ubuntu 22.04.1 LTS x86_64. code example:

        if (strcmp(argument, ubuntu) == 0)
        {
                system("do stuff");
        }
        else if (strcmp(argument, mac) == 0)
        {
                system("do other stuff")
        }
Start answered 22/8, 2022 at 3:10 Comment(6)
This answer does not add any useful information that is not already contained in the top-rated answer. The line if(!strcmp(argv[1], "ex1")) is equivalent to if(strcmp(argv[1], "ex1") == 0 ). Note the ! inside the expression.Heaviside
Actually you have no idea what you are talking about, any number OTHER THAN 0 results in a failure case. strcmp will find a match when asked for a failed result and will only behave properly according to the defined characteristics of true and false when compared to 0. if you think that a logical NOT before a function means that it is supposed to return true then you have semantics based issues that are fundamental to your misunderstanding of the language.Start
What I wrote in my previous comment is correct. The function strcmp will return -1 if the first string appears lexicographically before the second string and 1 if it appears afterwards. It returns 0 if both strings are identical. See the documentation of the function for further information. Since you want the if condition to be true (i.e. nonzero) instead of false (i.e. zero) when both strings are identical, you can either apply ==0 or ! to the return value of strcmp. Both operations are equivalent.Heaviside
==0 is a True case and ! denotes a logical NOT aka false. Please get this correct.Start
Please look at this code: onlinegdb.com/eobK4UK2-N as you can see, when you run strcmp without a logical NOT the success case returns 0. If you don't check for 0 it will not return on a success case. What you are doing is turning a success case into an unsuccessful case return value and expecting that unsuccessful value. It is confusing in the context of C return value semantics. If you are ever using that output for a stdout return value it will be hard to debug because it wont return 0 on a success case.Start
Please explain what you mean with the terms "success case" and "failure case". Do you mean "true" and "false"? Or do you mean "strings are identical" and "strings are not identical"? I am unable to understand your comments, because it is unclear to me what you mean.Heaviside

© 2022 - 2024 — McMap. All rights reserved.