Question

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

Was it helpful?

Solution 2

This is correct:

void const** args

I have a feeling that warning is a bug.

OTHER TIPS

There is no way to allocate an array of const pointers through simple malloc. 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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top