strdup makes a copy of a character string. Use it when your program needs to trash a string that you want to keep safe.
Code:
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
int foo(char *keepme)
{
char *working=strdup(keepme);
if(working != NULL)
{
/*
code to work with or possibly trash working string goes here (strtok is an example
*/
free(working);
}
else
{
perror("Insufficient memory");
exit(EXIT_FAILURE);
}
return 1;
}