Sign in to follow this  

C Precedence Question

This topic is 4335 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Given this function..... void strcpy4(char dest[], const char source[]) { while (*dest++ = *source++); } Does the increment from the ++ operator happen before the boolean test in the while or after? So when the source reaches '\0', the order is: 1. assignment of '\0' to destination 2. check for FALSE ('\0' is the equivalent of boolean false) 3. end the while without incrementing since it tests FALSE Is this correct? It doesn't do the increment prior to the boolean check, correct?

Share this post


Link to post
Share on other sites
Quote:
Original post by Palidine
it does increment _after_ the check. what you have, however, isn't a boolean check it's an assignment.

boolean check is == not =

also if your arrays are different sizes, your app will crash

-me


It's a string copy function. It's actually meant to be an assignment. It takes advantage of the fact I can do a boolean check this way. It's not checking the equality of the two strings in comparison, it's copying source to destination and breaking when it hits '\0', which is logical false.

Share this post


Link to post
Share on other sites
Quote:
Original post by Spoonbender
i++: i is incremented after use
++i: i is incremented before use.

Hope that answers your question :)


Yup, thanks. Just wanted to make sure nothing funky was happening. Sometimes a bit of code will work in a compiler even though it's breaking the C rules.

Share this post


Link to post
Share on other sites

This topic is 4335 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this