Jump to content
  • Advertisement
Sign in to follow this  
Ned_K

C Precedence Question

This topic is 4621 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
Advertisement
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

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
P.S. I'm only using this tortured syntax to understand precedence, not to write a real function in a real program.

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
Guest Anonymous Poster
Yeah, that's pretty much the canonical way to implement strcpy() in C.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!