How to copy a string into a char array in C++ without going over the buffer

前端 未结 11 979
Happy的楠姐
Happy的楠姐 2020-12-08 03:13

I want to copy a string into a char array, and not overrun the buffer.

So if I have a char array of size 5, then I want to copy a maximum of 5 bytes from a string in

11条回答
  •  难免孤独
    2020-12-08 03:39

    i think snprintf() is much safe and simlest

    snprintf ( buffer, 100, "The half of %d is %d", 60, 60/2 );
    

    null character is append it end automatically :)

提交回复
热议问题