• Limonene@lemmy.world
    link
    fedilink
    arrow-up
    0
    ·
    26 days ago

    C when I cast a char * * to a char * * const: ok

    C when I cast a char * * to a char * const *: ok

    C when I cast a char * * to a char const * *: WTF

    C when I cast a char * * to a char const * const *: ok

    • xep@fedia.io
      link
      fedilink
      arrow-up
      0
      ·
      26 days ago

      The WTF case isn’t allowed because it would allow modification of the const. From https://en.cppreference.com/w/cpp/language/implicit_conversion

      int main() { const char c = ‘c’; char* pc; char** ppc = &pc; const char** pcc = ppc; // Error: not the same as cv-unqualified char**, no implicit conversion. *pcc = &c; *pc = ‘C’; // If the erroneous assignment above is allowed, the const object “c” may be modified. }