How to Use the Strcpy Function in C++

The C++ strcpy function copies a string from a source location to a destination location and provides a null character to terminate the string. The destination should be large enough to contain the string, including the null terminator, to avoid an overflow. It should not overlap with the source because strcpy does not allocate storage. The following steps will help you use the strcpy function in C++.

Instructions

    • 1

      Learn the syntax of strcpy. The complete syntax is char *strcpy (char *destination, const char *source);.

    • 2

      Know that the destination is a pointer to an array and the source is a null-terminated string. The destination is returned.

    • 3

      Understand that the C++ strcpy function is kept in the cstring library. You may need to include the string.h header file to use this function.

    • 4

      Look at the following complete program for some simple examples of how to use strcpy:

      #include
      #include

      int main ()
      {
      char string1[]="test string";
      char string2[80];
      char string3[80];
      char *string4;
      strcpy (string2,string1);
      strcpy (string3,"strcopy worked.");
      string4 = strcpy(string2, "strcpy return example");
      printf ("string1: %s\nstring2: %s\nstring3: %s\n",string1,string2,string3);
      printf ("string4: %s\n",string4);
      return 0;
      }

    • 5

      Observe the following output for this program:

      string1: test string
      string2: test string
      string3: strcpy worked
      string4: strcpy return example

      The first strcpy shows a trivial example of copying the string pointed to by string1 into the array pointed to by string2. The second strcpy copies a literal to the destination array. Note that we must allocate storage for string2 and string3. The third strcpy illustrates a use of the return value of strcpy.

Related Searches:

Resources

Comments

You May Also Like

  • How to Use the Strncat Function in C++

    The C++ strncat function appends up to a specified number of characters to a destination string. The null terminator character in the...

  • How to Use the Strcat Function in C++

    Observe the following output for this program: string1: This string is concatenated. string2: Roses smell like old shoes. The first strcat is...

  • How to Use the Strstr Function in C++

    The C++ strstr function locates a specified substring within a source string. The scan does not include terminating null-characters. Strstr returns a...

  • How to Use the Memcpy Function in C++

    The memcpy function in C++ copies the specified number of bytes of data from the specified source to the specified destination. This...

  • How to Use the Memcmp Function in C++

    The C++ memcmp function compares a specified number of bytes of two blocks of memory. It returns 0 if they all match...

  • How to Use the Strncpy Function in C++

    The C++ strncpy function copies a specified number of characters from a source to a destination. A null character is not appended...

  • How to Use the Memmove Function in C++

    The memmove function in C++ copies the specified number of bytes of data from the specified source to the specified destination. This...

  • How to Prevent Memory Leaks in C++

    A memory leak is a type of programming bug that occurs when a program allocates more memory than it frees. This way,...

  • How to Display Fonts in MFC

    The Microsoft Foundation Class (MFC) Library lets a programmer plan the font display of an application through the CFont class. CFont encapsulates...

  • Microsoft C++ Tutorials

    Microsoft Visual C++ is a widely used programming language for building applications found on the Web, and desktop and mobile computers. Visual...

  • How to Use the Memset Function in C++

    Look at the following complete program for some simple examples of how to use memset. Note the use of memset to initialize...

  • How to Use C Arrays in C++

    A C array is a data structure that stores many elements of the same data type. C arrays, when you use them...

  • How to Use the Strchr Function in C++

    The C++ strchr function locates the first occurrence of a specified character in a source string and returns a pointer to that...

  • How to Initialize Variables in C

    C does not initialize variables automatically, so if you do not initialize them properly, you can get unexpected results. Fortunately, C makes...

  • How to Create a Dynamic String in C++

    Dynamic data types do not have a fixed size. When a computer stores a number, it uses the same number of bytes...

  • How to Use Turbo C

    Turbo C is the Integrated Development Environment (IDE) for the C programming language created and sold by the Borland Corporation. Originally founded...

  • How to Declare a Function in C

    Functions in C are the key to manageable structured programming. Every good program is written by taking the task and dividing it...

  • How to Declare Static Functions in C++

    Static member functions occur infrequently in C++ programs. A programmer would use these functions mainly when he wants to access a member...

Related Ads

Featured