Different types of const in pointers

Different types of const in pointers#

The keyword const can be used on two different places in a pointer declaration.

const char *const LICENSE = "CC BY-SA 4.0";
// `*` means a pointer, or in other words, memory address instead of value.
// So, `surname` is a pointer to a string.
// `const char` makes each character read-only (1. const).
// `*const` means a read-only pointer, `LICENSE` cannot be assigned to something
// else (2. const).

Above example does not make sense. We can define such a fixed string better as:

const char LICENSE[] = "CC BY-SA 4.0";

We typically use pointers, if we want to point to somewhere else during runtime. So when would we use a constant pointer (*const)?

I got the following answer from moonshotai/kimi-k2-instruct:

typedef struct {
    const char *const name;
    uint32_t        value;
} rom_item_t;

static const rom_item_t rom_table[] = {
    { .name = "baud",   .value = 115200 },
    { .name = "parity", .value = 0      },
};

Above example cannot be implemented with a flexible array:

typedef struct {
    const char name[];   /* ❌ illegal: flexible array not at end */
    uint32_t   value;
} rom_item_t;