Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      memccpy

      From cppreference.com
      <c‎ |string‎ |byte
       
       
       
       
      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:

      • count bytes are copied
      • the byte(unsignedchar)c is found (and copied).

      Thesrc anddest objects are interpreted as arrays ofunsignedchar.

      The behavior is undefined ifany condition is met:

      • access occurs beyond the end of thedest array;
      • the objects overlap (which is a violation of therestrict contract)
      • eitherdest orsrc is an invalid or null pointer

      Contents

      [edit]Parameters

      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

      [edit]Return value

      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.

      [edit]Notes

      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

      [edit]Example

      Run this code
      #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

      [edit]See also

      copies one buffer to another
      (function)[edit]
      copies a certain amount of wide characters between two non-overlapping arrays
      (function)[edit]
      moves one buffer to another
      (function)[edit]
      copies one string to another
      (function)[edit]
      concatenates two strings
      (function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/string/byte/memccpy&oldid=159915"

      [8]ページ先頭

      ©2009-2025 Movatter.jp