1、memcpy
void * memcpy ( void * destination, const void * source, size_t num );
複製內存塊
從source指向的位置直接複製num個字節的值到destination指向的內存塊。
對於這個函數,source和destination指向的對象的底層類型是無關的;結果是數據的一個二進制複製。
這個函數不檢查任何結束空字符(null)—— 它總是準確地複製num個字節。
參數
- destination
- 指向內容被複制到的目的數組的指針,類型被轉換爲void*。
- source
- 指向複製數據的源數組的指針,類型被轉換爲void*。
- num
- 要複製的字節數。
返回值
返回目的指針destination。
Example:
/* memcpy example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str1[]="Sample string";
char str2[40];
char str3[40];
memcpy (str2,str1,strlen(str1)+1);
memcpy (str3,"copy successful",16);
printf ("str1: %s\nstr2: %s\nstr3: %s\n",str1,str2,str3);
return 0;
}
Output:
str1: Sample string
str2: Sample string
str3: copy successful
2、memmove
void * memmove ( void * destination, const void * source, size_t num );
移動內存塊
從source指向的位置複製num個字節的值到destination指向的內存塊。複製操作的發生就好像使用了中間緩存,允許destination和source重疊。
對於這個函數,source和destination指向的對象的底層類型是無關的;結果是數據的一個二進制複製。
這個函數不檢查任何結束空字符(null)—— 它總是準確地複製num個字節。
參數
- destination
- 指向內容被複制到的目的數組的指針,類型被轉換爲void*。
- source
- 指向複製數據的源數組的指針,類型被轉換爲void*。
- num
- 要複製的字節數。
返回值
返回目的指針destination。
Example:
/* memmove example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] = "memmove can be very useful......";
memmove (str+20,str+15,11);
puts (str);
return 0;
}
Output:
memmove can be very very useful.
3、memsetvoid * memset ( void * ptr, int value, size_t num );
填充內存塊
參數
- ptr
- 指向需要填充的內存塊的指針。
- value
- 要設置的值,這個值是作爲int型傳遞的,但是該函數用這個值的unsigned char類型轉換值來填充內存塊。
- num
- 要是指特定值的字節數。
返回值
返回指針ptr。
Example:
/* memset example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] = "almost every programmer should know memset!";
memset (str,'-',6);
puts (str);
return 0;
}
Output:
------ every programmer should know memset!
4、memchr
const void * memchr ( const void * ptr, int value, size_t num ); void * memchr ( void * ptr, int value, size_t num );在內存塊中定位字符
在ptr指向的沒存塊的前num個字節中搜索value值得第一次出現(被解釋爲一個unsigned char類型的值),並返回指向該值的指針。
參數
- ptr
- 指向需要被執行搜索的內存塊的指針。
- value
- 需要被定位的值。這個值是作爲int型傳遞的,但是該函數用這個值的unsigned char類型轉換值來進行逐位的搜索。
- num
- 要是分析的字節數。
返回值
返回指向ptr指向的內存塊中value值第一次出現位置的指針。
如果沒有找到value值,則該函數返回NULL。
Example:
/* memchr example */
#include <stdio.h>
#include <string.h>
int main ()
{
char * pch;
char str[] = "Example string";
pch = (char*) memchr (str, 'p', strlen(str));
if (pch!=NULL)
printf ("'p' found at position %d.\n", pch-str+1);
else
printf ("'p' not found.\n");
return 0;
}
Output:
'p' found at position 5.
5、memcmpint memcmp ( const void * ptr1, const void * ptr2, size_t num );
比較兩個內存塊
參數
- ptr1
- 指向內存塊的指針。
- ptr2
- 指向內存塊的指針
- num
- 需要比較的字節數。
返回值
返回一個指示兩個內存塊的內容間的關係的整型值。
0值表示兩個內存塊的內容是相等的。
一個大於0的值表示,兩個內存塊中第一個不匹配的字節是ptr1的比ptr2的大,作爲unsigned char類型比較的;小於0則表示相反。
Example:
/* memcmp example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str1[256];
char str2[256];
int n;
printf ("Enter a sentence: "); gets(str1);
printf ("Enter another sentence: "); gets(str2);
n=memcmp ( str1, str2, 256 );
if (n>0) printf ("'%s' is greater than '%s'.\n",str1,str2);
else if (n<0) printf ("'%s' is less than '%s'.\n",str1,str2);
else printf ("'%s' is the same as '%s'.\n",str1,str2);
return 0;
}
Output:
Enter a sentence: building
Enter another sentence: book
'building' is greater than 'book'.