Simulate Pascal's/Delphi's Insert in C In Pascal/Delphi, there's a built-in function for inserting a substring into a main string.

procedure Insert ( const InsertStr : string; var TargetStr : string; Position : Integer ) ;


I'm used to this function, so I'd like to simulate it in C.

Because the main string will generally become longer after an insertion, the way to proceed is: 


To have some extra space in a char array allocated in stack, or
To use dynamically allocated strings.


I've chosen the second option. At the moment my code looks like this. It uses the standard C string.h library.

#include "stdlib.h"
#include "string.h"

void myStrInsert(const char *pWord, char **pStr, int pos)
{
    // Only dynamically allocated strings are allowed.
    if (pos &lt; 0)
        return;
    int lenStr = strlen(*pStr);
    if (pos &gt; lenStr)
        pos = lenStr;
    int lenWord = strlen(pWord);

    char *pResult = (char *)malloc(lenStr + lenWord + 1);
    if (pResult != NULL) {
        memcpy(pResult, *pStr, pos);
        memcpy(pResult + pos, pWord, lenWord);
        memcpy(pResult + pos + lenWord, *pStr + pos, lenStr - pos + 1);
        free(*pStr);
        *pStr = pResult;
    }
}


Can this function be simplified/optimized?
There are a number of things that I see that may help you improve your code.

Use the appropriate form for #includes

The code has the appropriate #include files but they should be in angle brackets and not quotes.  When you write #include "math.h" it is different from #include &lt;math.h&gt;.  For standard headers, you should use the &lt;&gt; form.  If you're not sure about the difference, see this question for more detail.  

Don't use Hungarian notation

Prefixing every variable with an abbreviation of its type (e.g. pStr) is usually called "Hungarian notation" and it was once popular.  Even then it was a bad idea.  Don't clutter up your source code with that; instead, concentrate on defining meaningful names for each variable and choose types appropriately.

Return an error code

The myStrInsert function can detect errors, but gives no indication to the calling program that anything went wrong.  A more robust design would return an error code.

Avoid memory fragmentation

The code allocates new space even if the original space would have had room.  This is somewhat wasteful and can lead to memory fragmentation.  Better would be to use realloc.

Do more error checking

It's good that pos is checked for a negative number, but it might be good to also check to assure that the pointers are not NULL before dereferencing them.

A possible rewrite

Using all of these suggestions, one might rewrite to something like this:

int myStrInsert(const char *inserted, char **target, int pos)
{
    if (inserted == NULL || target == NULL || *target == NULL || pos &lt; 0) {
        return -2;
    }
    size_t wordlen = strlen(inserted);
    size_t origlen = strlen(*target);
    char *orig = realloc(*target, origlen+wordlen+1);
    if (orig == NULL) {
        return -1;
    }
    if (pos &gt; origlen) {
        pos = origlen;
    } else {
        memmove(&amp;orig[pos+wordlen], &amp;orig[pos], origlen-pos);
    }
    memmove(&amp;orig[pos], inserted, wordlen);
    orig[origlen+wordlen] = '\0';
    *target = orig;
    return 0;
}


Limitations

One thing I think I should mention is that, unlike the original code, this code is not robust if we attempt to splice a string into itself.  That is, if we try to insert "fox" into the string "the fox" by passing in the first parameter as a pointer into the same string we're modifying, it's not necessarily well-behaved.  It's something that isn't hard to fix, but I wanted to make sure that readers were aware of the limitation before attempting to use this code.