Updated: April 19, 2023 |
Create a duplicate of a string, up to a maximum length
#include <string.h> char* strndup( const char* src, size_t size );
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
The strndup() function calls malloc() to allocate up to size + 1 bytes of memory, copies at most size bytes from the original string, terminates the new string with a NUL character, and returns a pointer to the new string.
A pointer to a copy of the string, or NULL if an error occurred (errno) is set.
#include <stdio.h> #include <string.h> #include <stdlib.h> int main( void ) { char *dup; dup = strndup( "Make a copy", 10 ); printf( "%s\n", dup ); free (dup); return EXIT_SUCCESS; }
Safety: | |
---|---|
Cancellation point | No |
Interrupt handler | No |
Signal handler | No |
Thread | Yes |