CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2007
    Location
    Lithuania
    Posts
    98

    [RESOLVED] Just curious about std::string

    Good evening lads.

    OK. Lets say I make string

    Code:
    std::string dataBlock;
    ...........................................
    //Fill that string with some data
    .........................................
    and presume that at some point I make

    Code:
    dataBlock[i] = '\0';
    So what happens there. Does the compile see this place/address where was dataBlock[i] set to '\0', as empty memory and can writte there new stuff or not ?

    I hope you understand what I mean. OC its g++.
    Share and always try to give back more.

  2. #2
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    Re: Just curious about std::string

    std::string can contain embedded '\0'. its just like any other char.
    just make shure you never write to a location >= size().
    Kurt

  3. #3
    Join Date
    Dec 2007
    Location
    Lithuania
    Posts
    98

    Re: Just curious about std::string

    Ahh OK. Thank you Zuk
    Share and always try to give back more.

  4. #4
    Join Date
    Feb 2009
    Posts
    326

    Re: Just curious about std::string

    couple of points:

    1) '\0' is called the Null character.
    2) This null character is used to mark the end of the ctring ( char [] or char * )
    3) The std :: string doesn't treat null character any differently from any other char

    Note - std :: string is a class with a lot of overloaded operator for easier use.

    The following shows the above

    Code:
    #include <iostream>
    #include <string>
    using std :: cout;
    using std :: endl;
    using std :: string;
    
    int main()
    {
        string dataBlock = "abcdefh";
    
        dataBlock[2] = '\0';
    
        dataBlock[3] = 'z';
    
        cout << "dataBlock = " << dataBlock << endl;
    
        return(0);
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured