C言語 memcpy関数の使い方のサンプル

概要
memcpy関数は指定バイト数分のメモリをコピーする関数です。

書式
#include <string.h>
void *memcpy(void *buf1, const void *buf2, size_t n);

第1引数にコピー先のメモリブロックのポインタ
第2引数にコピー元のメモリブロックのポインタ
第3引数はコピーサイズ

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char bufa[] = "STUDYSKILL";
char bufb[] = "56789012";
//3バイトコピー
memcpy(bufa,bufb,3);
//表示
printf("コピー後のbuf文字列→%s\n",bufa);
return 0;
}
#include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { char bufa[] = "STUDYSKILL"; char bufb[] = "56789012"; //3バイトコピー memcpy(bufa,bufb,3); //表示 printf("コピー後のbuf文字列→%s\n",bufa); return 0; }
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
      char bufa[] = "STUDYSKILL";
      char bufb[] = "56789012";

      //3バイトコピー
      memcpy(bufa,bufb,3);

      //表示
      printf("コピー後のbuf文字列→%s\n",bufa);
      

      return 0;
}

 

IT

Posted by arkgame