qh_strdup()

Updated: April 19, 2023

Safely create a duplicate of a string

Synopsis:

#include <qh/string.h>
char* qh_strdup(const char *str,
                const char *fallback_str)

Arguments:

str
The string to duplicate, if non-NULL.
fallback_str
The string to duplicate if non-NULL and str is NULL.

Library:

qh

Description:

The qh_strdup() function creates a duplicate of the string pointed to by str (or pointed to by fallback_str if str is NULL) and returns a pointer to the new copy. If both str and fallback_str are NULL, it returns NULL and sets errno to EINVAL.

Returns:

A pointer to a copy of a non-NULL (either str or fallback_str), or NULL if both str or fallback_str were NULL. When returning NULL, errno is always set, either to EINVAL if both strings were NULL, or to a standard errno if an error occurred while creating the duplicate string.