Thread: Using a vector element as a string

  1. #1
    Registered User
    Join Date
    Dec 2007

    Using a vector element as a string

    I would like to use a function that takes a char array[] as argument.
    Would it be possible to pass a vector element as a char array[] ?

    vector <string> vLines;
    CopyMemory((, &iLength, sizeof(WORD));
    SendMessage(hWin_2, EM_GETLINE, i, (LPARAM) (;
    cout << vLines[0] <<endl;
    Windows 10 with Visual Studio 2019 and MingW.

  2. #2
    Registered User
    Join Date
    Jan 2005
    You can pass a vector<char> as a char array. You can pass a string as a const char array. You can't pass a string as a non-const char array.

    Your CopyMemory call is weird, it doesn't match what I would expect it to be if you're using Windows, so it's hard to say what you're doing. The line in red is correct if you're not changing the string (e.g. if it's the source). If you are changing the string, you might consider something like:
    unsigned buffer_length = ???; // set buffer_length dynamically here
    vector<char> buffer(buffer_length); // create buffer of appropriate size
    My_Function(&buffer[0], buffer_length); // fill buffer from function, buffer_length); // asign buffer contents to your string
    Depending on what My_Function does there might be better solutions. For example if the buffer can be filled with a shorter, null-terminated string, you don't want to use assign there, you would make sure there was at least one null character at the end and assign with operator=.
    Last edited by Daved; 11-02-2009 at 04:11 PM.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Thanks a lot Daved!

    Actually im trying to rewrite this function to c++ and im wondering what to use.

    Dont know if i need vector of vector or just vector?

     int iCount;
         WORD iLength, iOffset;
         TCHAR **Lines;
         //Get Number of Lines in Edit Field
         iCount = SendMessage(hEdit, EM_GETLINECOUNT, 0, 0);
         //If Editbox is empty, exit function
         Lines = new TCHAR*[iCount];
         //Populate 2D array, Lines[LineIndex][LineText]
         for(int i=0; i<iCount; i++)
         {    iOffset = SendMessage(hEdit, EM_LINEINDEX, i, 0);
              iLength = (WORD)SendMessage(hEdit, EM_LINELENGTH, iOffset, 0);
              Lines[i] = new TCHAR[iLength+sizeof(WORD)+1];
              CopyMemory(Lines[i], &iLength, sizeof(WORD));
              SendMessage(hEdit, EM_GETLINE, i, (LPARAM)Lines[i]);
              Lines[i][iLength] = '\0';
    Windows 10 with Visual Studio 2019 and MingW.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Ladies and gents let me present to you the happiest man on earth: me.

    I solved the problem!

    Ok first of all i realised that i didnt need multidirectional array.

    Then i used Cpjust's help in another thread and i solved the problem with a vector <char>.

    string vs dynamic char array

    Cpjust: vec.push_back( '\0' ); // I can't remember if this is required, but it doesn't hurt.

    Apparrently its working without the '\0'. At least i dont have garbage at the end and nothing missing either.
    Last edited by Ducky; 11-03-2009 at 07:25 AM.
    Windows 10 with Visual Studio 2019 and MingW.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Slicing problem with inherited classes.
    By Swerve in forum C++ Programming
    Replies: 6
    Last Post: 10-20-2009, 02:29 PM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM