Thread: Initializing an array of char pointers directly

  1. #1
    Guest
    Guest

    Initializing an array of char pointers directly

    I'm learning OpenGL using the C API and some of the functions' argument types have proven a bit challenging to me.

    One example is the function
    Code:
    glShaderSource(GLuint shader, GLsizei count, GLchar const** string, GLint const* length);
    It resides in foo() which receives a vector "data" from elsewhere
    Code:
    void foo(std::vector<std::pair<GLenum, GLchar const*>> const& data);
    To pass the pair's second element to glShaderSource's third argument, I do the following:
    Code:
    GLchar const* const source[] = {data[0].second};
    glShaderSource(..., ..., source, ...);
    Now I have two questions regarding this:

    1. Can I initialize a char const** via initialization list, the way I do a char const*?
    Code:
    // this works
    std::vector<std::pair<GLenum, GLchar const*>> const shader_sources = {
        {GL_VERTEX_SHADER, "sourcecode"},
        {GL_FRAGMENT_SHADER, "sourcecode"}
    };
    
    // but is this possible?
    std::vector<std::pair<GLenum, GLchar const**>> = { ??? };
    2. Is there an alternative to creating a temporary GLchar**, even though that's specifically what the function wants?
    Last edited by Guest; 11-24-2014 at 05:21 PM.

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Well, you could just take the address of your data[0].second.
    It will work because the shader text is not needed after it is compiled.

    This is what I did:
    https://github.com/manasij7479/gl/bl...ork/shader.cpp
    Last edited by manasij7479; 11-24-2014 at 05:35 PM.

  3. #3
    Guest
    Guest
    Thank you for the good suggestion! I had to retrace why taking the address works, but now I understand. And it's cleaner than passing a GLchar** around, so I'll keep the std::vector as is

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. initializing a array of pointers
    By ueg1990 in forum C Programming
    Replies: 9
    Last Post: 03-16-2012, 06:56 AM
  2. help with initializing a char array
    By csepraveenkumar in forum C Programming
    Replies: 10
    Last Post: 03-22-2011, 01:25 AM
  3. trouble initializing a dynamic array of pointers to NULL
    By supernater in forum C++ Programming
    Replies: 8
    Last Post: 09-13-2009, 04:47 PM
  4. initializing char array inside a struct
    By panos in forum C Programming
    Replies: 6
    Last Post: 06-01-2007, 06:43 PM
  5. Initializing char * array
    By Tia in forum C Programming
    Replies: 6
    Last Post: 03-11-2003, 05:19 PM

Tags for this Thread