How to pass an std::string to glShaderSource?

后端 未结 7 1827
感情败类
感情败类 2020-12-05 04:47

I have the following code:

glShaderSource(shader, 1, (const char **)data.c_str(), NULL);

But it makes my program crash. How do I convert

7条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-05 05:10

    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:

    const char* mychararr[1] = {data.c_str()};
    glShaderSource(shader, 1, mychararr, NULL);
    

    That should work nicely as long as you don't leave the scope of mychararr.

提交回复
热议问题