[英]Array of const void * pointers and Visual C++
I have a problem with defining an array of const void *
pointers in ths code snippet - Visual C++ complains about whatever combination I try: 我在代码片段中定义
const void *
指针数组时遇到问题-Visual C ++抱怨我尝试的任何组合:
void *call_func_cdecl(void *func, const void *const *args, int nargs);
void vlogprintf(const char *format, va_list va) {
int nargs;
void **args;
args = malloc(...);
args[0] = format;
// fill the rest...
call_func_cdecl((void*)logprintf, args, nargs);
free(args);
}
As you know free
takes a void *
so the array itself should not be constant but its elements should be becase format
is a const void *
and it's an element of args
. 如您所知
free
带一个void *
所以数组本身不应该是常量,但是其元素应该是大小写format
为const void *
并且它是args
的元素。
So far I tried these: 到目前为止,我尝试了这些:
const void **args
Got warning C4090: 'function' : different 'const' qualifiers
at the free(args)
line 得到
warning C4090: 'function' : different 'const' qualifiers
在free(args)
行使用warning C4090: 'function' : different 'const' qualifiers
void const **args
Same as above 同上
void *const *args
Got error C2166: l-value specifies const object
at the args[0] = format
line 得到
error C2166: l-value specifies const object
在args[0] = format
行处error C2166: l-value specifies const object
void **const args
Same as above 同上
There is no way to allocate an array of const pointers through simple malloc. 无法通过简单的malloc分配const指针数组。 The basic types are what they are and if you know what you are doing, you can (more or less) safely disregard these errors.
基本类型就是它们的类型,如果您知道自己在做什么,则可以(或多或少)安全地忽略这些错误。 If you want to do it the "right" way, you may try something like this (code in no real-use order, just random snippets):
如果您想以“正确”的方式进行操作,则可以尝试执行以下操作(不按实际使用顺序编码,而只是随机片段):
struct constvoid {
const void * ptr;
}
void *call_func_cdecl(void *func, struct constvoid *args, int nargs);
{
struct constvoid* args = malloc(...);
args[0].ptr = format;
//fill the other
call_func_cdecl((void*)logprintf, args, nargs);
free(args);
}
This is correct: 这是对的:
void const** args
I have a feeling that warning is a bug. 我感觉警告是一个错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.