2

I'm looking at a function glMultiDrawElements in OpenGL and it defines one of it's parameters as having this type: const GLvoid * const *. Obviously GLvoid is just void but my question is what does the 2nd const even mean? Can it be ignored and if so can someone shed some light on why it's done this way.

https://www.opengl.org/sdk/docs/man4/html/glMultiDrawElements.xhtml

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
smack0007
  • 11,016
  • 7
  • 41
  • 48

1 Answers1

6

In this construction

const GLvoid * const *.

the second qualifier const means that pointer const GLvoid * is a const pointer. That is it is a constant pointer that points to a const object of type GLvoid.

This parameter declaration

const GLvoid * const * indices

means that using pointer indices you may not change the pointer (or pointers if this pointer points to the first element of an array of pointers) it points to.

Consider the following example

#include <stdio.h>

void f( const char **p )
{
    p[0] = "B";
}

int main( void )
{
    const char * a[1] = { "A" };
    f( a );

    puts( a[0] );
}    

This function will be compiled successfully and you can change the value of a[0].

However if you rewrite the program the following way

#include <stdio.h>

void f( const char * const *p )
{
    p[0] = "B";
}

int main( void )
{
    const char * a[1] = { "A" };
    f( a );

    puts( a[0] );
}    

The compiler issues an error like

prog.c:10:10: error: read-only variable is not assignable
    p[0] = "B";
    ~~~~ ^
1 error generated.
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335