On this page
  1. strdup
  2. Include
  3. Signature & Overview
  4. Notes & Pitfalls
  5. Example

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

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