C++ strcpy() function

 

C++ introduction by Gaurav Dixit

strcpy() function

Defined in header <cstring>
char* strcpy( char** dest, const* char** src );


Copies the character string pointed to by src, including the null terminator, to the character array whose first element is pointed to by dest.

The behavior is undefined if the dest array is not large enough. The behavior is undefined if the strings overlap.

Parameters

dest - pointer to the character array to write to
src - pointer to the null-terminated byte string to copy from

Return Value

dest

#include<iostream>
#include <cstring>
#include <memory>
using namespace std;

int main()
{
const char* src[]="Take the test.";
// src[0] = 'M'; // can't modify string literal
auto dst = make_unique<char[]>(strlen(src)+1);
// +1 for the null terminator
strcpy(dst.get(), src);
dst[0] = 'M';
cout<<src<<endl<<dst.get()<<endl;
return 0;
};

Output Result:


Take the test.
Make the test.

Post a Comment

Previous Post Next Post