I have the following code:
glShaderSource(shader, 1, (const char **)data.c_str(), NULL);
But it makes my program crash. How do I convert std::string
into const char **
?
I also tried (const char **)&
but it said "requires l-value" which I don't understand. It works fine when I use this code:
const char *data = "some code";
glShaderSource(shader, 1, &data, NULL);
But I can't make it work directly from a std::string
. I could allocate a new char
array for it but that is not nice code.
I also tried with const GLchar
but obviously it makes no difference.
Shader.cpp
Shader.hpp
I only want to point out that the pointer returned by
c_str()
is only valid as long as you don't do anything that requires reallocation of the internal buffer of std::string. That invalidates the pointer you got.But since you really require a
**
i would do this:That should work nicely as long as you don't leave the scope of mychararr.
You can get a reasonable-looking call by using a helper class. Define this class:
Then, when you need to call
glShaderSource
, do it this way:data.c_str()
returns aconst char*
, so do this:Try using the .c_str() it give you a char * that you can use as it worked for you b4
Using string's address and casting it also works in one line:
glShaderSource(vertexShader, 1, (const char* const*)&vertexSource, NULL);