C 庫 - memmove() 函式



C 庫的 memchr() 函式用於將一塊記憶體從一個位置複製到另一個位置。通常,此函式指定從源位置到目標位置的資料位元組數。

語法

以下是 C 庫 memchr() 方法的語法:

void *memmove(void *dest_str, const void *src_str, size_t numBytes)

引數

此函式接受以下引數:

  • *dest_str - 這是目標陣列的指標,內容將被複制到其中。它被型別轉換為 void* 型別的指標。

  • *src_str - 這是第二個指標,表示要複製的資料來源。它也被型別轉換為 void* 型別的指標。

  • numBytes - 此引數指的是要複製的位元組數。

返回值

此函式返回指向目標的指標,即 *dest_str。

示例 1

以下是用於更改字串位置的基本 C 庫函式 memchr()

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

int main () {
   char dest_str[] = "oldstring";
   const char src_str[]  = "newstring";
   printf("Before memmove dest = %s, src = %s\n", dest_str, src_str);
   memmove(dest_str, src_str, 9);
   printf("After memmove dest = %s, src = %s\n", dest_str, src_str);
   return(0);
}

輸出

以上程式碼產生以下輸出:

Before memmove dest = oldstring, src = newstring
After memmove dest = newstring, src = newstring

示例 2

在下面的示例中,我們對目標字串使用 puts() 方法。此方法在字串末尾新增換行符並返回整數值。在將內容從源字串複製到目標字串時,我們使用了 memmove() 方法。

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

int main() 
{ 
	char dest_str[] = "Tutorialspoint"; 
	char src_str[] = "Tutors";

	puts("source string [src_str] before memmove:-"); 
	puts(dest_str); 

	/* Copies contents from source to destination */
	memmove(dest_str, src_str, sizeof(src_str)); 

	puts("\nsource string [src_str] after memmove:-"); 
	puts(dest_str); 
	return 0; 
}

輸出

執行以上程式碼後,我們得到以下輸出:

Source string [src_str] before memmove:-
Tutorialspoint

Source string [src_str] after memmove:-
Tutors
廣告