click below
click below
Normal Size Small Size show me how
CYB2200 - L4
C Style Strings
| Question | Answer |
|---|---|
| bounded string functions are | safer alternatives to unbounded string functions |
| what makes bounded string functions | A length parameter to designate the length (or bounds) of the destination buffer |
| can bounded string functions be misused and how | May be misused in more subtle ways |
| strcpy() function | char *strcpy(char *dst, char *src) |
| strcpy() purpose | The strcpy() copies the string pointed to by src (including the terminating null character) into the array pointed to by dst and eases copying when it encounters an end of string character (a NUL byte) |
| what is important to remember about strcpy() | It ceases copying when it encounters an end of string character (a NUL byte) |
| strncpy() function | char *strncpy(char *dst, char *src, size_t n) |
| strncpy() purpose | strncpy() copies the string located at src to the destination dst, until it encounters an end of string character (a NUL byte) or when n characters have been written to the destination buffer. |
| bounded string functions | strncpy(), strncat() |
| what happens if missing NUL at end? | A lot of string manipulating functions use NUL terminator to identify the end of a string - these won't work as intended |
| example of string functions using NUL terminator to identify the end of the string | strncpy() copies the string located at src to the destination dst, until it encounters an end of string character (a NUL byte) or when n characters have been written to the destination buffer. |
| After calling strncpy(), you can add NUL terminator using something like | strncpy(buf, str, n); //assume n is the correct size of buf if (n > 0) buf[n - 1]= '\0'; |
| strcat() function | char *strcat (char *dst, char *src) |
| strcat() purpose | The strcat() functions are responsible for concatenating two strings together. The src string is appended to dst where The terminating character at the end of dst is replaced by the first character of src. |
| what is important to remember about strcat() | The terminating character at the end of dst is replaced by the first character of src. |
| strncat() function | char *strncat(char *dst, char *src, size_t n) |
| strncat() purpose | strncat() concatenates two strings together. The string src points to is appended to the string dst points to. It copies at most n bytes. The size argument represents how many bytes remain in the buffer. |
| what is important to remember about strncat() | The size argument represents how many bytes remain in the buffer. |
| what are two common mistakes when using strncat() | • 1. Supplying the size of the entire buffer instead of the size remaining in the buffer. • 2. The size doesn’t account for the trailing NUL byte |
| strlen() | returns the given byte string not including NUL terminator |
| some systems (BSD, Solaris exc) provide the following functions | strlcpy(), strlcat() |
| strlcpy() function | is a BSD specific extension to the libc string APIs |
| what does strlcpy() do | it copies at most size-1 bytes to dst |
| strlcat() | BSD specific extension to the libc string API |
| what does strlcat() do | the size parameter for strlcat() is the total size of the destination buffer instead of the remaining space left in the buffer (aka strncat()) |
| what does strlcat() accomplish | guarantees NUL termination of the destination buffer |
| what does strlcpy() accomplish | guarantees NUL byte termination of the destination buffer |