-2

I'm having trouble using strcmp in C.

I'm trying to compare a program's arguments using strcmp but even though the strings are the same it doesn't work. Here is the portion of code.

while(strcmp(argv[i], "-e") != 0)

So for i = 11 if I print the value of argv[i] I get

printf("String %s i %d", argv[i],i);

>> String -e i 11

But the while keeps on going. Any ideas why this is happening?

Code:

while(strcmp(argv[i], "-e") != 0 || i != argc)
{
    printf("String %s i %d", argv[i],i);
    if(!isdigit((unsigned char)*argv[i]) && strcmp(argv[i], "-t") != 0)
    {
        archivo = fopen(argv[i] , "r");
        TOT_IMG = TOT_IMG + 1;
        for(t=0;t<NUM_FUNC_TRAZO;t++)
        {
            for(d=0;d<NUM_FUNC_DIAMETRICA;d++)
            {
                for(c=0;c<NUM_FUNC_CIRCO;c++)
                {
                    if (fscanf(archivo, "%s",el) != EOF)
                    {
                        par->vector_circo[t][d][c] = strtod(el,NULL);
                        par->clase = clase;
                    }
                    else
                    {
                        break;
                    }
                }
            }
        }
        par_temp = par;
        par->siguiente = (parametros_lista) malloc(sizeof(parametros_elem));
        par = par->siguiente;
        par->anterior = par_temp;
    }
    else
    {
        if(strcmp(argv[i], "-t") != 0)
        {
            clase = atoi(argv[i]);
            CLASES = CLASES + 1;
        }
    }
    i = i + 1;
}
3
  • 1
    Sure you didn't forget to i++;? Commented Mar 22, 2013 at 21:54
  • 3
    Paste the full code rather than two excerpts with no explanation of how they're connected. Commented Mar 22, 2013 at 21:55
  • Nope I'm following the value of i with the debugger and it's fine. I'll post the rest of the code. Commented Mar 22, 2013 at 21:56

2 Answers 2

2

Let's look at this:

while(strcmp(argv[i], "-e") != 0 || i != argc)

OK, so let's assume strcmp correctly returns 0 when argv[i] is "e". We'll assume this because it's exceedingly unlikely that there's a bug in your library implementation of strcmp.

What happens if strcmp returns 0? Well, things don't just stop, your code checks whether i != argc is true. Is it? My psychic debugging skills tell me that you should look into that second part of the while.

You may also want to note that it's possible that your code could, potentially, access argv[argc], which is NULL. You may get lucky if strcmp is lenient when the input is NULL, but it's a bug that you should fix.

Sign up to request clarification or add additional context in comments.

4 Comments

I would add that while(i < argc && strcmp(argv[i], "-e") != 0) is probably a better choice here.
hmmm ok, but I'm using "||" on the while statement so the first part of the while should act independently from the second part shouldn't it? I'm going to delete the second part to check it
@Omri: Cool, you learn something new every day. Thanks, I'll edit accordingly.
@Atirag think of how || operates - if the left hand side is false, it checks the right, if the left hand side is true, it doesn't check the right hand side.
1

I'd rather recommend you to use getopt (3). This is widely used approach to parameters parsing conforming with POSIX.

Also there was another question related to achieving getopt.h interface on windows: getopt.h: Compiling UNIX C-Code in Windows. What's important it is answered (Xgetopt) so portability should be not a case.

5 Comments

getopt is NOT a part of the C standard. Its POSIX.
@cli_hlt POSIX defines the "POSIX C standard library".
IEEE POSIX has nothing to do with the ANSI C standard. Google will explain it to you. @Roman you should clarify your answer there.
@cli_hit where did you found word 'ANSI' even in original post (OK, now I at all deleted word 'standard' to not feed trolls)? Yes, it's POSIX, isn't it standard? Especially for you: en.wikipedia.org/wiki/C_POSIX_library
@RomanNikitchenko POSIX is a standard, it defines POSIX compliant operating systems. POSIX compliant operating systems(!) require a POSIX C library. *nix is such an OS, for example. OTOH, Windows is not. ANSI C is the standard of the C programming language. ANSI C conformant programs run on whatever system that has an ANSI C conformant compiler. ANSI C has also a standard library, which together with the language description defines the programming language C. It is very important not to mix these things up.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.