Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

zackriggle

Using strncpy()

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

Alright, I just want to know if strncpy() stops if it finds a null-terminator in the source string. My reason for this is that I may need a BYTE (u_char) pointer to store binary data at some time (actually, I know I will need it) and want to be able to copy that data to another BYTE buffer. Should I just make my own function that uses a for() loop and copies all of the data (or as much data as specified by a length parameter)? Or, will strncpy() work?

Share this post


Link to post
Share on other sites
Advertisement
quote:

The strncpy function copies the initial count characters of strSource to strDest and returns strDest. If count is less than or equal to the length of strSource, a null character is not appended automatically to the copied string. If count is greater than the length of strSource, the destination string is padded with null characters up to length count. The behavior of strncpy is undefined if the source and destination strings overlap.

Share this post


Link to post
Share on other sites
Okay then, my own function it is. Someone please just glance at this and tell me if it will work:

typedef unsigned char BYTE
typedef unsigned long DWORD
void bincpy(BYTE* dest, const BYTE* source, DWORD length)
{
for(DWORD x = 0; x != length; x++)
dest[x] = source[x];
};


This is supposing that dest and source are the exact same length

Share this post


Link to post
Share on other sites
Why don''t you try memcpy. It copies memory(or rather data) from one place to another.

void* memcpy(void* dest, const void* src, size_t num);

Share this post


Link to post
Share on other sites

  • 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!