Function rcutils_string_array_init

Function Documentation

rcutils_ret_t rcutils_string_array_init(rcutils_string_array_t *string_array, size_t size, const rcutils_allocator_t *allocator)

Initialize a string array with a given size.

This function will initialize a given, zero initialized, string array to a given size.

Note that putting a string into the array gives owenship to the array.

Example:

rcutils_allocator_t allocator = rcutils_get_default_allocator();
rcutils_string_array_t string_array = rcutils_get_zero_initialized_string_array();
rcutils_ret_t ret = rcutils_string_array_init(&string_array, 2, &allocator);
if (ret != RCUTILS_RET_OK) {
  // ... error handling
}
string_array.data[0] = rcutils_strdup("Hello", &allocator);
string_array.data[1] = rcutils_strdup("World", &allocator);
ret = rcutils_string_array_fini(&string_array);
Parameters:
  • string_array[inout] object to be initialized

  • size[in] the size the array should be

  • allocator[in] to be used to allocate and deallocate memory

Returns:

RCUTILS_RET_OK if successful, or

Returns:

RCUTILS_RET_INVALID_ARGUMENT for invalid arguments, or

Returns:

RCUTILS_RET_BAD_ALLOC if memory allocation fails, or

Returns:

RCUTILS_RET_ERROR if an unknown error occurs.