🧵 str
Painfully common string utilities for C
Loading...
Searching...
No Matches
cstr.h File Reference
#include <stddef.h>

Go to the source code of this file.

Functions

ptrdiff_t strzcpy (char *restrict dest, const char *restrict src, size_t size)
 Similar to strncpy(), except result is always null-terminated.
ptrdiff_t strzcat (char *restrict dest, const char *restrict src, size_t size)
 Similar to strncat(), except result is always null-terminated.
char * strdup (const char *src)
 Duplicates a null-terminated string.
char * strndup (const char *src, size_t size)
 Duplicates a null-terminated string, up to a maximum length.

Function Documentation

◆ strdup()

char * strdup ( const char * src)

Duplicates a null-terminated string.

The returned string can be passed into free() to destroy it.

Parameters
srcThe null-terminated string to duplicate.
Returns
A pointer to the newly allocated string, or a null pointer if an error occurred.

◆ strndup()

char * strndup ( const char * src,
size_t size )

Duplicates a null-terminated string, up to a maximum length.

If a null terminator is not encountered in the first size bytes, it is appended to the duplicated string.

The returned string can be passed into free() to destroy it.

Parameters
srcThe null-terminated string to duplicate.
sizeThe maximum number of bytes to copy from src.
Returns
A pointer to the newly allocated string, or a null pointer if an error occurred.

◆ strzcat()

ptrdiff_t strzcat ( char *restrict dest,
const char *restrict src,
size_t size )

Similar to strncat(), except result is always null-terminated.

Parameters
destPointer to the null-terminated string to append to
srcPointer to the buffer to copy from
sizeThe maximum permitted length of the string in dest, including the null terminator.
Returns
Remaining free space in dest. If the string was truncated, this amount will be negative, whose absolute value is equal to the number of characters that did not fit.

◆ strzcpy()

ptrdiff_t strzcpy ( char *restrict dest,
const char *restrict src,
size_t size )

Similar to strncpy(), except result is always null-terminated.

Parameters
destPointer to the buffer to copy to
srcPointer to the buffer to copy from
sizeThe maximum permitted length of the string in dest, including the null terminator.
Returns
Remaining free space in dest. If the string was truncated, this amount will be negative, whose absolute value is equal to the number of characters that did not fit.