Copy end of string in C

Church picture Church · Sep 20, 2012 · Viewed 23.8k times · Source

I am trying to use strncpy to only copy part of a string to another string in C.

Such as:

c[] = "How the heck do I do this";

Then copy "do this" to the other string, such that:

d[] = "do this"

Help is appreciated.

Answer

Daniel Fischer picture Daniel Fischer · Sep 20, 2012

Just pass the address of the first letter you want to copy: strcpy(dest, &c[13]).

If you want to end before the string ends, use strncpy or, better, memcpy (don't forget to 0-terminate the copied string).