Search code examples
carrayspointersdynamic-memory-allocationconst-correctness

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:

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.

So far I tried these:

  • const void **args

    Got warning C4090: 'function' : different 'const' qualifiers at the free(args) line

  • void const **args

    Same as above

  • void *const *args

    Got error C2166: l-value specifies const object at the args[0] = format line

  • void **const args

    Same as above


Solution

  • This is correct:

    void const** args
    

    I have a feeling that warning is a bug.