Bare with me here. I am still lost on doing 'word' problems -- understanding syntax of code and what the problem asks for is not my forte - YET.

I need to do the following: write a function that uses dyn mem allocation to create a copy of a string. The call of such function would be : p = strdup(str);

Function is to :
(1) allocate space for a string of the same length as str
(2) copy the contents of str into the new string
(3) return a pointer to it
(4) return a null pointer if the memory allocation fails

Is any of the following OK?
Whatever is wrong or unnecessary, can you step me through the logic of it, so I can correct my
error(s)...and thus learn this correctly.

Hey and what should I set up in the main() to actually check this code for myself???

Code:
char *strdup(const char *str)
{

     char *result;
     
     result = malloc(strlen(str)+1);
     if (result == NULL)  {
        printf("Error: malloc failed ");
        return NULL;
     }

     strcpy (result, str);
     return result;

}