Strcpy

From Hackepedia
Jump to navigationJump to search

strcpy is part of standard libc in the c programming language. strcpy(3) can be found in the development manual pages.

strncpy(name, "t", 4") -->  name = "t\0\0\0"
strncpy(name, "tim", 4") --> name = "tim\0"
strncpy(name, "dave", 4)  --> name = "dave"  (note, no \0)
strncpy(name, "benjamin", 4" --> name = "benj" (still no \0)

strn* or snprintf will ensure you will never exceed SIZE, but if you are less than SIZE it pads it with \0. This will prevent buffer overflows. If you're using a BSD based system, check out strlcpy which is preferred over strncpy.