#include #include #include #include int main () { const char * read_only = "monkey"; char * duplicate; char * three_dup; duplicate = strdup (read_only); /* We can write to this string. */ duplicate[0] = toupper (duplicate[0]); printf ("%s -> %s\n", read_only, duplicate); /* Now copy three characters. */ three_dup = strndup (read_only, 3); /* It actually allocates four bytes, since a trailing 0 is added. */ printf ("%s -> %s\n", read_only, three_dup); /* We need to free it when the program finishes. */ free (duplicate); free (three_dup); return 0; }