Function

GLibstrndup

Declaration

gchar*
g_strndup (
  const gchar* str,
  gsize n
)

Description

Duplicates the first n bytes of a string, returning a newly-allocated buffer n + 1 bytes long which will always be nul-terminated. If str is less than n bytes long the buffer is padded with nuls. If str is NULL it returns NULL. The returned value should be freed when no longer needed.

To copy a number of characters from a UTF-8 encoded string, use g_utf8_strncpy() instead.

Parameters

str const gchar*
 

The string to duplicate.

 The data is owned by the caller of the function.
 The string is a NUL terminated UTF-8 string.
n gsize
 

The maximum number of bytes to copy from str.

Return value

Returns: utf8
 

A newly-allocated buffer containing the first n bytes of str, nul-terminated.

 The caller of the function takes ownership of the data, and is responsible for freeing it.
 The string is a NUL terminated UTF-8 string.