Using memcpy to copy data in memory - C++ Data Type

C++ examples for Data Type:char array

Description

Using memcpy to copy data in memory

Demo Code

#include <iostream> 
#include <cstring> // memcpy prototype 
using namespace std; 

int main() /*from www  .ja  v  a2s  .c  o  m*/
{ 
    char s1[ 17 ]; 

    // 17 total characters (includes terminating null) 
    char s2[] = "Copy this string"; 

    memcpy( s1, s2, 17 ); // copy 17 characters from s2 to s1 

    cout << "After s2 is copied into s1 with memcpy,\n" 
        << "s1 contains \"" << s1 << '\"' << endl; 
    
    return 0;
}

Result


Related Tutorials