Const Char *variable vs Const Char variable[] and glShaderSource()

Hello Everyone,

I really enjoyed programming in school and decided to try and pick it back up. I don't think I've ever fully understood pointers/references or when to use * or &. It seems straight forward when I read about it, but then I find examples that defy my understanding. I'm going to focus on one example for now.

const char *vShaderSource = 
"#version 460 core\n"
"layout (location=0) in vec3 aPos;\n"
"void main()\n"
"	gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"


glShaderSource(vShader, 1, &vShaderSource, NULL);

I've played around with this code and have found my code variable has to be declared exactly like that or it doesn't work, but I don't know why. How would I have figured this out without someone telling me directly?

What is the difference between
const char *vShaderSource vs const char vShaderSource[] ?
As far as I can tell they act the same. But I get the following error using vShaderSource[]
main.cpp:58:49: error: cannot convert 'const char (*)[120]' to 'const GLchar* const*' {aka 'const char* const*'} in argument passing
glShaderSource(vShader, 1, &vShaderSource, NULL);

Why does char variable[] work fine but char *variable doesn't? (I removed const in front)

glShaderSource(vShader, 1, &vShaderSource, NULL);
I'm also confused why & is in front of vShaderSource here. I thought & was used to pass the memory address of a non-pointer variable, but we declared this variable as a pointer. It seems it wants to pass the memory address of vShaderSource but for a pointer variable wouldn't that be accomplished with just vShaderSource? Does it have something to do with const GLchar **string being the requirement for that argument? (I'm not sure what the 2 ** signifies either).
Last edited on
but I don't know why. How would I have figured this out without someone telling me directly?

-- that is not easy. You are out of pure C++ and off in a library, and for those, an example / documentation is the right way to get to using it. You may figure it out yourself, but it takes a lot of digging in their source code to do so, and experience as well to unravel what you see there. That experience is not a small thing, talking years of using the language here.

const char *vShaderSource vs const char vShaderSource[]
the first one is a pointer. Pointers can make use of dynamic memory (they do not have to, but they can). The second one is an array. Arrays are fixed in size (this gets hard to explain.. a pointer once allocated is also fixed in size, but you can smoke and mirrors allocate a new block of memory, move the old data to it, and have it 'grow' or shrink in size). Arrays are a lot like a pointer and in fact the language will convert an array's name into a pointer for you, to save you having to type a little more every time you do that. Arrays and pointers are very similar: they share a lot of syntax and they are both a way to manage a block of memory. They are however different things, and that matters at times, and other times, it does not matter at all.

& has multiple uses. It is bit-wise and (unrelated, eg 0001 and 0010 is 0 bit-wise and 1(true) logic wise (logical is &&) ). That is unrelated. & is also a reference, eg int &x = y; //x is a new name for y. It is also address of (take a pointer to this thing). Here, it its taking an address of an existing item because the function using it wants a pointer, so you get a pointer to the thing to match the requirement of the function.

** is a pointer to a pointer. Pointers are actually a type of variable, and a kind of integer, and therefore they also have a location in memory. ** is the address of a pointer that is itself an address of some data somewhere.
** can be used as a type of 2-d construct.
it can also just mean an array of pointers. A typical example of this is C style strings, where ** usually means ONE dimensions of C-style strings, where a C-style string is itself just a char pointer.
that is hard to follow, so some code:
char cstring[100]; // a c-style string.
char manystrings[20][100]; //if an array of char is one string, this is 20 strings.
the same with pointers,
if char* cp is one string, then
char ** mcp can be many strings.

If you do not understand these things, you should put the GL library aside and work on just grasping pointers for a while. They are complex and take people a while to get a good handle on (bad pun). Pointers are less used in modern c++ because many things use built in tools that hide the pointers (this is a good thing, it prevents a lot of bugs caused by misuse of pointers), but it is also good to have a solid understanding of them so you can use these old style libraries and tools that are pointer heavy.
Last edited on
I don't think I've ever fully understood pointers/references or when to use * or &.

Learn C++ has a good introduction to pointers and references starting at:
Thanks for the explanation Jonnin.

I will take your advice Furry Guy and just put the OpenGL aside for now.

Thanks again :-)
Topic archived. No new replies allowed.