Converting a std::list to char*[size]

前端 未结 6 1298
温柔的废话
温柔的废话 2021-01-21 20:54

for some reason I cannot explain, every single item in the character array...is equal to the last item added to it...for example progArgs[0] through progArgs[size] contains the

6条回答
  •  甜味超标
    2021-01-21 21:15

    You're setting progArgs[count] to the same item every time!

    int count = 0;
    char *progArgs[commandList.size()]
    
    for(list::iterator t=commandList.begin(); t!=commandList.end(); t++)
    {
        char * item = new char[strlen((*t).c_str()) + 1]; //create new character string
        strcpy(item, (*t).c_str()); //convert from const char to char
        progArgs[count] = item;
        count++;
    }
    

    Then remember to call delete[] for each element in progArgs.

    Personally, I'd create an array of string and convert to char * on the fly as needed.

提交回复
热议问题