1

I came across a code which extracted a token(c string) from a given string,but I am confused by the 5th line:tok && *tok. I just cannot figure out this operation's result. Does anybody happen to know?

const char* getfield(char* line, int num)
{
    const char* tok;
    for (tok = strtok(line, ";");
            tok && *tok;
            tok = strtok(NULL, ";\n"))
    {
        if (!--num)
            return tok;
    }
    return NULL;
}

Thank you!

JadenFero
  • 13
  • 5
  • this code has not sense (but is formally ok). Compute expression and be not interrest in result. Maybe wrong copy&paste? – Jacek Cz Apr 21 '18 at 18:23
  • 3
    `&&` is logical and. It's taking the logical and of `tok` (effectively `tok != NULL`) and `*tok` (effectively `*tok != 0`). – Stephen Newell Apr 21 '18 at 18:23
  • 1
    @StephenNewell is correct. In other words, the expression evaluates to `true` if and only if `tok` points to an non-nul character. – Jive Dadson Apr 21 '18 at 18:25
  • @Jacek Cz I don't understand it either... The codes aimed to read a CSV file. If you are curious about that...here is the link where I saw these codes: [link](https://stackoverflow.com/questions/12911299/read-csv-file-in-c#answer-12911465). Share me your opinion if you figure out what he is doing! – JadenFero Apr 22 '18 at 18:28

4 Answers4

5

It checks first if the pointer (tok) isn't nullptr (pointing to nothing), then if the pointer isn't nullptr, it checks if it points to memory where the value (*tok) isn't '\0'.

Jive Dadson
  • 16,680
  • 9
  • 52
  • 65
Sébastien S.
  • 1,444
  • 8
  • 14
  • Thank you! And one more thing, will error message occur if `tok` turns out to be a null pointer or is a `'\0'`? – JadenFero Apr 22 '18 at 18:36
  • You're welcome. If `num > 0` and `tok == NULL || *tok == '\0'` it will return `NULL`, else it will return `tok`. An error message can appear if you check the return value of the function when you call it and print a message if it returned `NULL`. – Sébastien S. Apr 22 '18 at 18:45
3

This is the conditional expression, and it is checking that tok is not nullptr and that *tok, or the character, is not the "nul" character '\0'.

In a C-style for-loop, you have the initalizer, the condition, and the increment expressions. The condition here is that tok evaluates to true, then *tok evaluates to true.

This can only occur if tok is not nullptr, and the character pointed to (*tok) is not '\0'.

Jive Dadson
  • 16,680
  • 9
  • 52
  • 65
Alex Huszagh
  • 13,272
  • 3
  • 39
  • 67
0

The meaning of && remains the same. It is still a logical operator. That statement is equivalent to

(tok != nullptr) && (*tok != '\0');
Jive Dadson
  • 16,680
  • 9
  • 52
  • 65
user1451348
  • 109
  • 4
0

Can any point out what the below portion means?

{
    if (!--num)
        return tok;
}

I'm most interested in the if (!--num). It's not making sense to me. In my head it reads as if NOT num = num - num. It's really cramping my brain.

I know ! is the NOT operator, but the --num just doesn't compute in my brain.

Rsp8
  • 133
  • 1
  • 10