C語言中的記憶體操作是什麼?
庫 #include <memory.h> 包含基本的記憶體操作。雖然不嚴格意義上的字串函式,但這些函式的原型在 #include <string.h> 中宣告。
這些記憶體操作如下:
void *memchr (void *s, int c, size_t n); | 在緩衝區中搜索字元。 |
int memcmp (void *s1, void *s2, size_t n); | 比較兩個緩衝區。 |
void *memcpy (void *dest, void *src, size_t n); | 將一個緩衝區複製到另一個緩衝區。 |
void *memmove (void *dest, void *src, size_t n); | 將一定數量的位元組從一個緩衝區移動到另一個緩衝區。 |
void *memset (void *s, int c, size_t n); | 將緩衝區的全部位元組設定為給定的字元。 |
請注意,在所有情況下,都是複製記憶體位元組。sizeof() 函式 又派上用場了。
memcpy(dest, src, SIZE); | 複製字元(位元組) |
memcpy(idest, isrc, SIZE*sizeof(int)); | 複製整數陣列 |
memmove() behaves in exactly the same way as memcpy() except, that the source and destination locations may overlap.
memcmp() is similar to strcmp() except here, unsigned bytes are compared and returns less than zero if si is less than s2 etc.
例如:
char src[SIZE], dest[SIZE]; int isrc[SIZE], idest[SIZE];
廣告