Jump to content
  • Advertisement
Sign in to follow this  
mdog1234

strcpy

This topic is 5016 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

Im having trouble understanding what is going on with strcpy. if I have a character array: char array[10] why can I put a string with larger than 10 characters into this array using strcpy? When I try it everything runs normally and nothing crashes.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by mdog1234
Im having trouble understanding what is going on with strcpy. if I have a character array: char array[10] why can I put a string with larger than 10 characters into this array using strcpy? When I try it everything runs normally and nothing crashes.

It doesn't crash now, but it might crash in the future. Whatever memory follows that array is getting overwritten, and if that happens to be important data, you can't get it back. Right now, you're getting luck [possibly because you're in a debug build, and arrays are padded to prevent such errors from causing crashes], but there's no telling when a problem might pop up.

Use strncpy. It takes an additional parameter that specifies how many characters you should actually copy. In your case, you'd call strncpy(array, src, 9). If src is twenty characters long, the first nine get copied into array, and then the null terminator is put in the tenth slot.

CM

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!