CHAPTER 04:EX 18

/*
18. Write a function that takes a char* argument. Using new,
dynamically allocate an array of char that is the size of
the char array that’s passed to the function. Using array
indexing, copy the characters from the argument to the
dynamically allocated array (don’t forget the null
terminator) and return the pointer to the copy. In your
main( ), test the function by passing a static quoted
character array, then take the result of that and pass it
back into the function. Print both strings and both
pointers so you can see they are different storage. Using
delete, clean up all the dynamic storage.
*/

// 他们所占用的内存空间是不一样的

#include
< iostream >
using   namespace  std;

int  main() {
    
char *  charcpy ( char * );
    
char  a[ 10 =   " abc " ;
    cout 
<<   & <<  endl;
    
char   * cp; 
    cp 
=  charcpy (a);
    cout 
<<   & cp  <<  endl;
    
char   * ncp  =  charcpy(cp);
    cout 
<<   & ncp  <<  endl;
}

char *  charcpy ( char *  cp) {
    
int  length  =   0 ;
    
char  t;
    
while  ((t  =  cp[length])  !=   '

你可能感兴趣的:(CHAPTER 04:EX 18)