CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2009
    Location
    Boston
    Posts
    375

    question about what assign() statement is doing

    Hello,

    I am looking at some code that uses assign,
    Code:
    typedef struct DLIST {
       char pcode[12];
    } DLIST;
    
    // instance of DLIST
    DLIST dlist;
    
    // string
    std::string sFieldCount;
    
    // dlist.pcode is populated elsewhere
    
    // assign value to string
    sFieldCount.assign( dlist.pcode, dlist.pcode+8 );
    I'm not clear on what the last line is doing here. I have used assign() before but I seem to remember this just being a simple value in quotes that was assigned to the string like, some_string.assign("something");

    From reading, it looks likes FieldCount.assign( dlist.pcode, dlist.pcode+8 ); is assigning a range of the character array pcode[] to the string FieldCount (where string is a vector of char). Does this assign the first 8 characters of dlist.pcode to sFieldCount or is it more complicated than this?

    Thanks,

    LMHmedchem

  2. #2
    Join Date
    Nov 2018
    Posts
    140

    Re: question about what assign() statement is doing

    It seems to do exactly what you think it should.

    https://en.cppreference.com/w/cpp/st..._string/assign

  3. #3
    Join Date
    May 2009
    Location
    Boston
    Posts
    375

    Re: question about what assign() statement is doing

    Thanks,

    I did look at the reference page but I often don't find std::basic_string<CharT,Traits,Allocator>::assign to be all that helpful. Maybe I just need to keep learning until I do.

    I find code examples to be more useful but end up in the weeds when I can't find something similar to what I am looking for. That's probably not the sign of an excellent programmer.

    From examples I have seen, I would have expected a range like,

    new_string.assign( other_string.begin(), other_string.begin()+8 );

    or

    new_string.assign( char_array[0], char_array[0]+8 );

    I guess that is what it says but I just don't recognize dlist.pcode in this statement,

    sFieldCount.assign( dlist.pcode, dlist.pcode+8 );

    as dlist.pcode.begin() etc.

    LMHmedchem

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,857

    Re: question about what assign() statement is doing

    dlist.pcode is effectively type char* pointer to beginning of pcode and dlist.pcode+8 is 8 bytes from the beginning of pcode. char_array[0] is the first byte of the array. Using this form you'd use:

    Code:
    new_string.assign( &char_array[0], &char_array[0]+8 );
    where & obtains the address of the first [0] element of the array.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

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