   [c++] vector string to const char**
  Note that the title doesnt display < and >I want to convert a vector<string> to a const char** without resorting to legacy C memory or file reading. background info:An OpenGL function requires me to give it the source code of a shader in the following format:void glShaderSource( GLuint  shader,  GLsizei  count,  const GLchar ** string,  const GLint * length  );const GLchar ** //string    Specifies an array of pointers to strings    containing the source code to be loaded into the    shader.I use C++ I/O Thanks in advance!   
  Something like this would do the trick I believe:vector<string> cppStrings = getStrings();vector<const char*> cStrings;for(int i = 0; i < cppStrings.size(); ++i){   cStrings.push_back(cppStrings.c_str());}const char** cStringArray = &cStrings[0];You can add some error checking for empty vectors, possibly throw in an algorithm for the string assignment, etc.  But this is the gist of what you need to do.   ;#include <string>#include <vector>#include <algorithm>#include <functional>#include <iterator>vector<string> lines;vector<const char*> starts;transform( lines.begin(), lines.end(), back_inserter( starts ), mem_fn( &string::c_str ) );const char** arr = &starts.front();EDIT:Ninja'd... And then the server broke down.   ;Quote:Original post by _fastcallEDIT:Ninja'd... And then the server broke down.Your solution is better anyway so I wouldn't consider it a ninja [smile]   ;  Thanks for the help so far! There is one little thing tho, I think getline doesnt add a "\0" terminator to the string because the lines end up not being terminated and the compiler cant compile the shader.. Code so far:void mSHADER::fromFile(GLhandleARB Handle, string const& Filename){ifstream file(Filename.c_str());if(!file.good()){log_shader.log << "Can't read shader source: " << Filename << endl;return;}string line;vector<string> lines;vector<const char*> shader;while(getline(file,line)){if((line.at(0) != '/')&&(!line.empty())){lines.push_back(line);}}shader.reserve( lines.size() );transform( lines.begin(), lines.end(), back_inserter( shader ), mem_fn( &string::c_str ) );glShaderSource(Handle, 1, &shader.front(), NULL);file.close();}I tried:line + "\0"andline.append("\0");but neither worked!   ;  c_str() returns a string that is terminated by a '\0', so there shouldn't be a problem.If you want each line to be terminated by a '\n', you can add that yourself:lines.push_back(line + "\n");   ;  Hmm that wasnt it as the shader debugger is reporting strange errors: ERROR: 0:2: 'pre-mature EOF' : syntax error parse errorERROR:  compilation errors.  No code generated.(The 0:2: indicates the line number)the shaders are just default shaders:void main(){gl_Position = ftransform();}and:void main(void){    gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);}the code is the same as I posted but with the added + "\n". Screenshot shows the shader code in memory as well. The model is bright white ingame   ;  Did you read the documentation for glShaderSource?vector<string> cppStrings = getStrings();vector<const char*> cStrings;vector<GLint> lengths;for(int i = 0; i < cppStrings.size(); ++i){   cStrings.push_back(cppStrings.c_str());   lengths.push_back(cppStrings.length());}glShaderSource(  ??,  cppStrings.size(),  &cStrings[0],  &lengths[0]);This doesn't need null terminated strings.;Quote:Original post by AntheusDid you read the documentation for glShaderSource?vector<string> cppStrings = getStrings();vector<const char*> cStrings;vector<GLint> lengths;for(int i = 0; i < cppStrings.size(); ++i){   cStrings.push_back(cppStrings.c_str());   lengths.push_back(cppStrings.length());}glShaderSource(  ??,  cppStrings.size(),  &cStrings[0],  &lengths[0]);This doesn't need null terminated strings.Yes I did but I thought it was easier to pass null terminated strings and NULL for the lengths. Anyways, its still not compiling correctly but I think this is an OpenGL error:ERROR: 2:1: 'gl_Position' : undeclared identifier ERROR: 2:1: 'ftransform' : no matching overloaded function found ERROR:  compilation errors.  No code generated.ERROR: 2:1: 'gl_Position' : undeclared identifier ERROR: 2:1: 'ftransform' : no matching overloaded function found ERROR:  compilation errors.  No code generated.These are still the simple shaders I got from the opengl wikiAny ideas before i post this in the OpenGL forums?   ;Quote:Original post by DefeatistAnyways, its still not compiling correctly but I think this is an OpenGL error:It is.Quote:Any ideas before i post this in the OpenGL forums?Might be a better place.