Summary of functions of c_str of data of copy of p n in string

  • 2020-04-02 01:32:05
  • OfStack

The string class of the standard library provides three member functions to get a c-type array of characters from a string: c_str(), data(), copy(p,n).

C_str () : generates a const char* pointer to a null-terminated array.

Note:
1.
The data in this array is temporary, and is invalidated when a member function is called that changes the data. So either convert it now or copy its data into memory that the user can manage. Pay attention to. Look at the case:


const char* c; 
string s="1234"; 
c = s.c_str();  
cout<<c<<endl; //Output: 1234
s="abcd"; 
cout<<c<<endl; //Output: the abcd

If you continue to use the c pointer above, the resulting error will be unthinkable. As in: 1234 into abcd

Actually, c = s.c1 STR (); Not a good habit. Since the contents of the c pointer are easy to fail, we should follow the above method, so how to copy the data out? This requires functions such as strcpy (recommended).


//const char* c; // 1.  
//char* c;       // 2.  
//char c[20];  
char* c=new char[20]; 
string s="1234"; 
//c = s.c_str();  
strcpy(c,s.c_str()); 
cout<<c<<endl; //Output: 1234
s="abcd"; 
cout<<c<<endl; //Output: 1234

Note: Const can no longer be written into a value as shown above. Also can not, the use of the uninitialized local variable "c", run will be wrong.

2. C_str () returns a client readable, immutable pointer to an array of characters without manually releasing or deleting the pointer.

2. Data (): similar to c_str(), but the returned array does not terminate with a null character.

3. Copy (p,n,size_type _Off = 0) : copies at most n characters from a string object into the space that the character pointer p points to. The default starts with the first character, but you can also specify the starting position (remember to start at 0). Returns the character that is actually copied from the object. -- the user needs to make sure that p points to enough space to save n characters.


// basic_string_copy.cpp 
// compile with: /EHsc /W3 
#include <string> 
#include <iostream> 

int main( ) 
{ 
    using namespace std; 
    string str1 ( "1234567890" ); 
    basic_string <char>::iterator str_Iter; 
    char array1 [ 20 ] = { 0 }; 
    char array2 [ 10 ] = { 0 }; 
    basic_string <char>:: pointer array1Ptr = array1; 
    basic_string <char>:: value_type *array2Ptr = array2; 

    cout << "The original string str1 is: "; 
    for ( str_Iter = str1.begin( ); str_Iter != str1.end( ); str_Iter++ ) 
        cout << *str_Iter; 
    cout << endl; 

    basic_string <char>:: size_type nArray1; 
    // Note: string::copy is potentially unsafe, consider 
    // using string::_Copy_s instead. 
    nArray1 = str1.copy ( array1Ptr , 12 );  // C4996 
    cout << "The number of copied characters in array1 is: "
        << nArray1 << endl; 
    cout << "The copied characters array1 is: " << array1Ptr << endl; 

    basic_string <char>:: size_type nArray2; 
    // Note: string::copy is potentially unsafe, consider 
    // using string::_Copy_s instead. 
    nArray2 = str1.copy ( array2Ptr , 5 , 6  );  // C4996 
    cout << "The number of copied characters in array2 is: "
        << nArray2 << endl; 
    cout << "The copied characters array2 is: " << array2Ptr << endl; 

    ////Make sure there is enough room for the array3
    //char array3[5]={0}; 
    //basic_string<char>::pointer array3Ptr=array3; 
    //basic_string<char>::size_type nArray3; 
    //nArray3 = str1.copy(array3,9); // Error!!  
    //cout<<"The number of copied characters in array3 is: " 
    //  <<nArray3<<endl; 
    //cout<<"The copied characters array3 is: "<<array3Ptr<<endl; 
}

Last comment out part of the above, although compiled without error, but the runtime errors: Stack around the variable 'array3' was corrupted.


Related articles: