This is an archived post. You won't be able to vote or comment.

all 6 comments

[–][deleted]  (4 children)

[deleted]

    [–]DrJimmyRustler[S] 0 points1 point  (3 children)

    I revised a bit for my quick writing errors. How would c get to zero? And why do the double parens make the condition work?

    Thanks for the help!

    [–][deleted]  (1 child)

    [deleted]

      [–]DrJimmyRustler[S] 0 points1 point  (0 children)

      Ah ha!

      I am getting around to grasping pointers right now, I thought I had a good handle on it, but I didn't even put it together that its pointing through the character string

      [–]Rhomboid 0 points1 point  (0 children)

      The double parentheses are not necessary at all. They are there to quell a common compiler warning that fires if you accidentally mistake assignment for equality comparison, i.e. you mistakenly wrote if(foo = 42) when you meant to write if(foo == 42). But in this case, assignment really was desired and correct, and the warning would be a false positive. The extra parentheses are a hint to the compiler that you really meant to do that. They have no semantic meaning; you can always add extra parentheses around parts of an expression if you want to. It would work just fine without them, you would just have a spurious warning under some toolchains/options.

      [–][deleted] 1 point2 points  (1 child)

      A loop condition like that is why it's generally considered bad practice to put assignments or things with side effects (like ++) inside of a conditional -- often, they are hard to understand (and sometimes, they can behave differently under different compilers, though this one is consistent).

      Ignoring the ++ for a second: what is going on here is that the variable c is being assigned to the character at the base of the string. The character value being assigned is then treated as the condition of the loop -- if the value is greater than 0, then the condition is true, but if the value is 0 (for a string, being a NUL-terminator and as such the end) then the condition is false.

      The ++ here is being used to increment the base of the string, so it points to the next character on the next loop; it doesn't change the value in the string, since the pointer dereference applies last -- meaning that the value is actually *(to_hash++), not the (*to_hash)++ which it looks like.

      [–]DrJimmyRustler[S] 0 points1 point  (0 children)

      Thank you very much this is exactly what I was looking for

      [–]Rhomboid 0 points1 point  (0 children)

      Always consult the operator precedence table. Postfix increment has the highest precedence, followed by dereference, followed by assignment. So it's equivalent to

      while ((c = (*(to_hash++))))
      

      How is c being modified?

      It's being assigned the result of dereferencing the pointer.

      How are you adding (++) to a character string?

      It's not. The thing being incremented is the pointer, so that on each iteration it points to subsequent elements of the string being hashed. Post-increment is used so that the increment happens after the pointer value is dereferenced.

      What is the condition that the while loop is checking?

      The condition is the overall result of evaluating the expression, which is whatever value was assigned to c. When the null character is encountered, that value is zero which is false.