C 庫函數(shù) - memcpy()

C 標準庫 - <string.h> C 標準庫 - <string.h>

描述

C 庫函數(shù) void *memcpy(void *str1, const void *str2, size_t n) 從存儲區(qū) str2 復制 n 個字符到存儲區(qū) str1

聲明

下面是 memcpy() 函數(shù)的聲明。

void *memcpy(void *str1, const void *str2, size_t n)

參數(shù)

  • str1 -- 指向用于存儲復制內(nèi)容的目標數(shù)組,類型強制轉換為 void* 指針。
  • str2 -- 指向要復制的數(shù)據(jù)源,類型強制轉換為 void* 指針。
  • n -- 要被復制的字節(jié)數(shù)。

返回值

該函數(shù)返回一個指向目標存儲區(qū) str1 的指針。

實例

下面的實例演示了 memcpy() 函數(shù)的用法。

#include <stdio.h>
#include <string.h>

int main ()
{
   const char src[50] = "http://www.o2fo.com";
   char dest[50];

   printf("Before memcpy dest = %s\n", dest);
   memcpy(dest, src, strlen(src)+1);
   printf("After memcpy dest = %s\n", dest);
   
   return(0);
}

讓我們編譯并運行上面的程序,這將產(chǎn)生以下結果:

Before memcpy dest =
After memcpy dest = http://www.o2fo.com

C 標準庫 - <string.h> C 標準庫 - <string.h>