9 char* astring
= strdup("this is a string # with something to seek");
10 size_t len
= strlen(astring
);
11 memccpy(astring
+10, astring
, '#', len
-10);
12 sprintf(astring
, "this is a string # with something to seek");
13 memccpy(astring
, astring
+10, '#', len
);
15 sprintf(astring
, "this is a string # with something to seek");
17 * space is earlier than len, no overlap
18 * "this " gets copied (up to and including the first ' ')
19 * and it overwrites the destination starting with the 's' of "string"
20 * so res will point to the 'g' of "string"
22 char* res
= memccpy(astring
+10, astring
, ' ', len
-10);
23 assert(res
&& *res
== 'g');
24 sprintf(astring
, "this is a string # with something to seek");
25 /* length is 0, nothing copied, returns NULL */
26 res
= memccpy(astring
, "abcdefhhijklmnopqrstuvwxy", 'z', 0);
28 /* 'z' not found so 20 bytes copied, returns NULL */
29 res
= memccpy(astring
, "abcdefhhijklmnopqrstuvwxy", 'z', 20);