On this page
strdup
Prev: strerror | Up: string.h | Next: strcpy
Rel: string.h, stdlib.h & string.h Guide
Ref: cppreference — strdup
Tags: #public #prog #c #stdlib #string #tier/standard
Include
#include <string.h>
Signature & Overview
char *strdup(const char *str); /* C23 / POSIX */
Allocates a heap copy of str (caller must free()); standardized in ISO C23.
Notes & Pitfalls
- POSIX / C23 heap allocation: Always check for
NULLand release withfree().
Example
#include <assert.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
char *copy = strdup("hello");
assert(copy != NULL && strcmp(copy, "hello") == 0);
free(copy);
}
Linked from
Related notes
- tuts/c/stdlib/string/strcpy Linked here; links to this page; shared tags: #c, #prog, #stdlib, #string, #tier
- tuts/c/stdlib/string/strerror Linked here; links to this page; shared tags: #c, #prog, #stdlib, #string, #tier
- tuts/c/stdlib/string Linked here; links to this page; shared tags: #c, #prog, #stdlib, #string
- tuts/c/stdlib/stdlib_and_string_h Linked here; shared tags: #c, #prog, #stdlib
- tuts/c/stdlib/string/memchr shared tags: #c, #prog, #stdlib, #string, #tier
- tuts/c/stdlib/string/memcmp shared tags: #c, #prog, #stdlib, #string, #tier
- tuts/c/stdlib/string/memcpy shared tags: #c, #prog, #stdlib, #string, #tier
- tuts/c/stdlib/string/memmove shared tags: #c, #prog, #stdlib, #string, #tier
- tuts/c/stdlib/string/memset shared tags: #c, #prog, #stdlib, #string, #tier
- tuts/c/stdlib/string/strcat shared tags: #c, #prog, #stdlib, #string, #tier