copy a const char* into array of char (facing a bug)



I have following method

static void setName(const char* str, char buf[16])
{
    int sz = MIN(strlen(str), 16);
    for (int i = 0; i < sz; i++) buf[i] = str[i];
    buf[sz] = 0;
}


int main()
{
   const char* string1 = "I am getting bug for this long string
greater than 16 lenght);
   char mbuf[16];
   setName(string,mybuf)
   // if I use buf in my code it is leading to spurious characters
since length is greater than 16 . 

Please let me know what is the correct way to code above if the restriction for buf length is 16 in method static void setName(const char* str, char buf[16])


Related to : copy a const char* into array of char (facing a bug)

Privacy Policy - Copyrights Notice - Feedback - Report Violation - RSS 2017 © bighow.org All Rights Reserved .