toCString

Creates C string allocated using tryAllocate with copy of str. If str is null returns null.

tryAllocate is assumed to return properly aligned for To memory or null if allocation fails.

If allocation fails toCString will call $(COREREF exception, onOutOfMemoryError) which is expected to throw an $(COREREF exception, OutOfMemoryError).

To*
toCString
(
alias tryAllocate
To = char
From
)
(
in From[] str
)
if (
isSomeChar!To &&
isSomeChar!From
)

Examples

t
{
	import core.stdc.stdlib;
	import core.stdc.string;

	string str = "abc";

	char* cstr = str.toCString!malloc();
	scope(exit) free(cstr);
	assert(strlen(cstr) == 3

Meta