1

I am using a while loop to detect the presence of a ">" symbol in an array. The loop conditional never evaluates to true. When I show the contents of the array, the ">" symbol is there by itself though. In my code, this isn't the first call to strtok. This is also only a snippet of the total code. The string to be broken up by strtok is "ls > testfile.txt". Any help would be appreciated.

while (userArgs[k] != ">") {

    k++;

    userArgs[k] = strtok(NULL, " ");
    if(userArgs[k] == ">") {
        break;

    }
    if (userArgs[k] == NULL){
        break;
    }
}
1
  • What is the dataype of userArgs? If it contains chars, you should be comparing to the char '>' not the string ">". Commented May 9, 2011 at 6:41

4 Answers 4

2

strtok returns a char * so I guess you're trying to compare strings.

You can't compare strings like that in C, use strcmp. What you are doing now is comparing addresses, definitely not what you want.

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

4 Comments

you are assuming userArgs is the char** passed to main??
Who is talking about main ? I only see userArgs. I don't care how he got it - he is assigning a char * to userArgs[k] so if that compiles, then I guess it must be able to hold a char *.
ah ok, I was looking at your comment in relation to the while conditional only... As that is what the question asks about... and you added the reference to strtok AFTER my comment. Still, why downvote a question?
@Aaron Gage Not a stellar question, to be sure, but I believe it isn't actually awful. The only problem with it is that it leaves out the declaration of userArgs. Meh, some people will downvote anything :-)
1

IF userArgs is of type char then you are comparing a string with a character constant while (userArgs[k] != ">") and if(userArgs[k] == ">") So actually the address of the strings in the binary program file is compared to the character value. Note that the ">" is a string constant and is represented by some address where the string is stored '>' is a character constant and has a value.

IF userArgs is char * then you are comparing the address of the userArgs[i] location in the program stack or heap (if dynamically allocated) with the address of ">" string stored in the program binary and loaded by the OS on the memory. These memory location will vary from time to time and has no relation with the contents of it. To compare the contents of the memories you have to use while (strcmp (userArgs[k], ">") != 0). OR to avoid calling strcmp you might want to do the following while ((userArgs[k][0] != '>') && (userArgs[k][1] == '\0')

The above examples would require change as per your needs.

1 Comment

Whoops guess I did forget a necessary part. userArgs is char *. Your strcmp suggestion works perfectly. Thanks!
1

Use '>' instead of ">". The first one is a character and the second one is a string - or actually a pointer to a character, and comparing pointers clearly isn't what you want.

Comments

0

You are comparing if the address userArgs[k] is the same as the stack address for "<". This is never the case. You would need to match userArgs[k][0] == '>' // note the single quote. but this wrong if the '>' is not preceded by a space.

1 Comment

The address of ">" is almost certainly not on the stack, and it's not "never the case" either: given userArgs[k] = ">", it is possible that userArgs[k] == "<" will compare true, but also possible that it will compare false. This is because the compiler is free to combine identical string literals.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.