Search code examples
cc99const-correctness

How to declare a constant "mutable pointer" to an immutable block of memory


I'm wondering how to declare, basically, (a const pointer to (a mutable pointer to (a const type))) in C99.

Let's say I have this call site:

const uint8_t* result;
create(&result);

Is void create(const uint8_t * const * resultPtr) the correct way to declare the callee in this case or does it mean something unintended? I don't need to reassign resultPtr and the caller shouldn't mess with [from this perspective] *resultPtr[0] but my function needs to assign *resultPtr = ….


Solution

  • Let's ask cdecl:

    $ cdecl declare p as const pointer to pointer to const char
    const char ** const p
    

    This declaration can be read entirely from right to left to obtain the English version (remembering that const char and char const are the same).