C++ program to Demonstrate Copying of String using Pointers














































C++ program to Demonstrate Copying of String using Pointers




Program explanation
Here the main part of program call copystr() function to copy str1 to str2 .
In this function the expression *dest++ = *src++; take the value at the address pointed to by src and places it in the address pointed to by dest. Both pointer are then incremented, so the next time through the loop the next character will be transferred . The loop terminate when the null character is found in src; at this point a null is inserted in dest and the function returns.
C++ Program to demonstrate string copying using pointers

#include<iostream>
using namespace std; int main() { void copystr(char* , const char*); //prototype char* str1 = "Copy string ! "; char str2[80]; copystr(str2, str1); //copy str1 to str2 cout<< str2 << endl; //display str2 return 0; } void copystr( char* dest, const char* src) { while( *src ) //until null character { *dest++ = *src++; //copy char from src to dest } *dest = '\0'; //terminate dest with null character }

Output
Copy string !


Comments