| Functions | |||||||||||||||||||||||||||||||||||||||||
| Character manipulation | |||||||||||||||||||||||||||||||||||||||||
| Conversions to and from numeric formats | |||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||
| String manipulation | |||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||
| String examination | |||||||||||||||||||||||||||||||||||||||||
| Memory manipulation | |||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||
| Miscellaneous | |||||||||||||||||||||||||||||||||||||||||
(C11)(C11) | |||||||||||||||||||||||||||||||||||||||||
Defined in header <string.h> | ||
void* memccpy(void*restrict dest,constvoid*restrict src,int c,size_t count); | (since C23) | |
Copies bytes from the object pointed to bysrc to the object pointed to bydest, stopping afterany of the next two conditions are satisfied:
Thesrc anddest objects are interpreted as arrays ofunsignedchar.
The behavior is undefined ifany condition is met:
Contents |
| dest | - | pointer to the object to copy to |
| src | - | pointer to the object to copy from |
| c | - | terminating byte, converted tounsignedchar at first |
| count | - | number of bytes to copy |
If the byte(unsignedchar)c was found,memccpy returns a pointer to the next byte indest after(unsignedchar)c. Otherwise it returns a null pointer.
The function is identical to thePOSIXmemccpy.
memccpy(dest, src,0, count) behaves similar tostrncpy(dest, src, count), except that the former returns a pointer to theend of the buffer written, and does not zero-pad the destination array. Thus,memccpy is useful for efficiently concatenating multiple strings.
char bigString[1000];char* end= bigString+sizeof bigString; char* p= memccpy(bigString,"John, ",'\0',sizeof bigString-1);if(p) p= memccpy(p-1,"Paul, ",'\0', end- p);if(p) p= memccpy(p-1,"George, ",'\0', end- p);if(p) p= memccpy(p-1,"Joel ",'\0', end- p);if(!p) end[-1]='\0'; puts(bigString);// John, Paul, George, Joel
#include <ctype.h>#include <stdio.h>#include <string.h> int main(void){constchar src[]="Stars: Altair, Sun, Vega.";constchar terminal[]={':',' ',',','.','!'};char dest[sizeof src];constchar alt='@'; for(size_t i=0; i!=sizeof terminal;++i){void* to= memccpy(dest, src, terminal[i],sizeof dest); printf("Terminal '%c' (%s):\t\"", terminal[i], to?"found":"absent"); // if `terminal` character was not found - print the whole `dest` to= to? to: dest+sizeof dest; for(char* from= dest; from!= to;++from)putchar(isprint(*from)?*from: alt); puts("\"");} puts("\n""Separate star names from distances (ly):");constchar*star_distance[]={"Arcturus : 37","Vega : 25","Capella : 43","Rigel : 860","Procyon : 11"};char names_only[64];char*first= names_only;char*last= names_only+sizeof names_only; for(size_t t=0; t!=(sizeof star_distance)/(sizeof star_distance[0]);++t){if(first) first= memccpy(first, star_distance[t],' ', last- first);elsebreak;} if(first){*first='\0';puts(names_only);}elseputs("Buffer is too small.");}
Output:
Terminal ':' (found): "Stars:"Terminal ' ' (found): "Stars: "Terminal ',' (found): "Stars: Altair,"Terminal '.' (found): "Stars: Altair, Sun, Vega."Terminal '!' (absent): "Stars: Altair, Sun, Vega.@" Separate star names from distances (ly):Arcturus Vega Capella Rigel Procyon
(C11) | copies one buffer to another (function)[edit] |
(C95)(C11) | copies a certain amount of wide characters between two non-overlapping arrays (function)[edit] |
(C11) | moves one buffer to another (function)[edit] |
(C11) | copies one string to another (function)[edit] |
(C11) | concatenates two strings (function)[edit] |