strcpy() in C++

Video Tutorial
FREE
Strlen,Strcpy methods thumbnail
This video belongs to
C++ Course: Learn the Essentials
14 modules
Certificate
Topics Covered

Overview

The strcpy function in C++ creates a copy of the string and replaces the other string with that copy, i.e., copies one string content to another. It is present in the cstring header file.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Syntax of strcpy() in C++

The syntax of this function is:

src is the string whose content is going to be copied, and dest is the string where the content is going to be copied.

Parameters of Strcpy() in C++

This function takes two input parameters as follows.

  • src: This is the pointer to the string to be copied. const char* type of source string ensures that the function will not change the content of the source string.
  • dest: This is the pointer to the array where the other string will be copied. dest is not of type const because the function changes the content of the destination string.

Return Value of Strcpy() in C++

The strcpy function in C++ returns a pointer to the dest string after copying the other string. Let's see an example to understand the strcpy function.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Example

Output

As we see from above, the output src string content gets copied to the dest string using the strcpy() function.

What is strcpy() in C++?

The strcpy function in C++ is used to copy the content of the source string to the destination string without changing the source string. It returns a pointer to the destination string after copying its content.

A few points we need to remember while using strcpy in C++ are as follows:

  • This function only takes C style strings (char str[]) in its input parameters. So it doesn't work with C++ style strings (string s).
  • Since the function creates a copy of the string referred to by the src pointer, it doesn't affect the source string.
  • The size of the destination string must be large enough to contain the source string(including the null character).
  • The destination string and the source string should not overlap in memory.

Output

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Conclusion

  • The strcpy function in C++ creates a copy of the string and replaces the other string with this.
  • It is defined in the cstring header file.
  • Both the input parameters must be C-style strings. This function doesn't accept C++ strings as input.
  • The size of the destination array must be large enough to contain the source array.
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more