3 ms·
Are you saying that in C a callee can cast a const pointer to non-const and write to it and that's sanctioned by the Standard?
by TwoBit 5y ago
Are you saying that in C a callee can cast a const pointer to non-const and write to it and that's sanctioned by the Standard?
- moonchild 5y agoYes, as long as the original pointer was to a non-const location. That is: void mutate(const int *x) { *(int*)x = 5; } int main() { int a; mutate(&a); // ok const int b; mutate(&b); // undefined behaviour }
- TwoBit 5y ago> Yes, as long as the original pointer was to a non-const location. So then actually no - it's not guaranteed to be portable. That non-const location qualification of course means portable code cannot just cast const pointers to non-const.