memcpy() in C++

memcpy(): This function is available in the file string.h. This function copies the values of num bytes from the location pointed to by the source directly to the memory block pointed to by destination.

Syntax:

void *memcpy(void *destination, const void *source, size_t num)

Parameters: Three parameters are required for this function.

destination: A Pointer to the destination array where the content is to be copied, type-casted to a pointer of type void*. 

source: A Pointer to the source of data to be copied, type-casted to a pointer of type const void*.

num: Number of bytes to copy. size_t is an unsigned integral type.

For Example:

source = "Hello World"

memcpy(destination,source,sizeof(source))  = > It copies all the characters of the source to destination.

Approach

C++

#include <bits/stdc++.h>
using namespace std;

int main()
{
    char destination[1000];
    char source[] = "Hello World";

    //copy contents of the source into destination
    memcpy(destinationsourcesizeof(source));

    cout << destination << "\n";

    return 0;
}


No comments:

Post a Comment