c - Copying an array of characters -


gcc 4.4.2 c89

I was just working on some signals. However, with the program below, I can not copy the source to the destination. Whenever I try and print for a loop, I can display the characters in the source, but the destination is empty. When the pointer returns, the destination is empty. So it has not copied anything.

I have stayed for about an hour for this problem, and it is not clear whether this is not doing the work.

Any suggestions?

Many thanks,

  Four strows_source [80] = "A string for demolition purposes"; Four str_dest [80] = {0}; Char * my_strncpy (char * dest, const four const * source, const size_t size) {size_t i = 0; Printf ("size [% d] \ n", size); For (i = 0; i  

============== Edit

  Four Straw_source [80] = "Democracy for a string Purpose "; Four str_dest [80] = {0}; Printf ("str_dest [% s] \ n", my_strncpy (str_dest, str_source, size (str_dest)); Char * my_strncpy (char * dest, const four const * source, const size_t size) {size_t i = 0; / * * Increase the increment and return the portion which will be the beginning of the array. * / Character * p = dest; / * Copy the specified amount (usually the maximum size of maximum of 1) * for / i (i = 0; i  

How exactly are you testing that the function does not work? Note that your function is returning dest which now indicates end of the destination buffer. The calling function is checking the returned indicator or is it the form of a destination buffer Checking the sign passed in?

For these reasons, * dest for printf is useless in your loop; If you have already raised dest at that point, then it points to the next unused location ( printf source also prints the copy to copy the next character, which you have not copied yet.)

On the one hand, it is not clear what you do Want to be size if this is the number of bytes in the destination buffer, then you can overflow buffer when you type NUL-terminator May include.

Oh, and you should see that you source .


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -