memcpy
من cppreference.com
معرفة في ملف <string.h>
|
||
void* memcpy( void *dest, const void *src, size_t count ); |
(حتى C99) | |
void* memcpy( void *restrict dest, const void *restrict src, size_t count ); |
(منذ C99) | |
تقوم بنسخ عدد count
من الحروف من الكائن المشار إليه بـ src
إلى الكائن المشار اليه بـ dest
. في حالة وجود تداخل بين موقع الكائنين في الذاكرة يكون السلوك غير معرف.
محتويات |
[تعديل] معطيات
dest | - | مؤشر على الموقع في الذاكرة الذي سيتم النسخ إليه |
src | - | مؤشر على الموقع في الذاكرة الذي سيتم النسخ منه |
count | - | عدد البايت التي سيتم نسخها |
[تعديل] القيمة المُرجعة
dest
[تعديل] مثال
قم بتشغيل هذا الكود:
#include <stdio.h> #include <string.h> #define LENGTH_STRING 20 int main(void) { char source[LENGTH_STRING] = "Hello, world!"; char target[LENGTH_STRING] = ""; int integer[LENGTH_STRING / sizeof(int)] = {0}; printf("source: %s\n", source); printf("target: %s\n", target); printf("integer: "); for (unsigned i = 0; i < sizeof(integer) / sizeof(integer[0]); ++i) { printf("%x ", integer[i]); } printf("\n========\n"); memcpy(target, source, sizeof source); memcpy(integer, source, sizeof source); printf("source: %s\ntarget: %s\n", source, target); printf("source(hex): "); for (unsigned i = 0; i < sizeof(source) / sizeof(source[0]); ++i) { printf("%02x ", source[i]); } printf("\n"); printf("integer(hex: %s-endian): ", integer[0] == 0x48656c6c ? "big" : "little"); for (unsigned i = 0; i < sizeof(integer) / sizeof(integer[0]); ++i) { printf("%08x ", integer[i]); } printf("\n"); }
خرج ممكن:
source: Hello, world! target: integer: 0 0 0 0 0 ======== source: Hello, world! target: Hello, world! source(hex): 48 65 6c 6c 6f 2c 20 77 6f 72 6c 64 21 00 00 00 00 00 00 00 integer(hex: big-endian): 48656c6c 6f2c2077 6f726c64 21000000 00000000
[تعديل] أنظر أيضا
تنقل محتوى مصفوفة إلى مصفوفة أخرى (دالة) | |
مقالة مرجع C++ عن memcpy
|