<分区>
我正在查看 OpenGL 中的函数 glMultiDrawElements,它定义其中一个参数具有这种类型:const GLvoid * const *
。显然 GLvoid 只是无效的,但我的问题是第二个 const
甚至是什么意思?它可以被忽略吗?如果可以,有人可以阐明为什么这样做。
https://www.opengl.org/sdk/docs/man4/html/glMultiDrawElements.xhtml
<分区>
我正在查看 OpenGL 中的函数 glMultiDrawElements,它定义其中一个参数具有这种类型:const GLvoid * const *
。显然 GLvoid 只是无效的,但我的问题是第二个 const
甚至是什么意思?它可以被忽略吗?如果可以,有人可以阐明为什么这样做。
https://www.opengl.org/sdk/docs/man4/html/glMultiDrawElements.xhtml
最佳答案
在这个结构中
const GLvoid * const *.
第二个限定符const
表示指针const GLvoid *
是一个const 指针。也就是说,它是一个常量指针,指向 GLvoid
类型的 const 对象。
这个参数声明
const GLvoid * const * indices
意味着使用指针 indices
您不能更改它指向的指针(或指针,如果该指针指向指针数组的第一个元素)。
考虑下面的例子
#include <stdio.h>
void f( const char **p )
{
p[0] = "B";
}
int main( void )
{
const char * a[1] = { "A" };
f( a );
puts( a[0] );
}
此函数将编译成功,您可以更改a[0]的值。
但是如果你按照下面的方式重写程序
#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] );
}
编译器发出类似这样的错误
prog.c:10:10: error: read-only variable is not assignable
p[0] = "B";
~~~~ ^
1 error generated.
关于c - "const void * const *"在 C 中是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31908118/