Testing it on my machine with the following code seems to validate this hypothesis.
//file: test.c
#include <stdio.h>
const void * const_pointer = &const_pointer;
void * const const_value = &const_value;
int main()
{
printf("%p\n", const_pointer);
*(int*)const_pointer = 0;
printf("%p\n", const_pointer);
printf("---------------------------\n");
printf("%p\n", const_value);
*(int*)const_value = 0;
printf("%p\n", const_value);
return 0;
}
Result:
$ gcc test.c
test.c:4:30: warning: initialization discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
void * const const_value = &const_value;
^
$ ./a.out
0x55b29ebfc010
0x55b200000000
---------------------------
0x55b29ebfbdb8
Command terminated
As to why the first one doesn't also result in a segfault, I don't know.In the second case the const_value variable itself is const-qualified and thus located in .rodata, but the pointer itself is not const-qualified so nothing prevents you from attempting to modify the data through that pointer. This is why you get a compiler warning about discarding the 'const' qualifier in the initialization. Since const_value is in .rodata, writing to it through the pointer causes a segfault.
As Sean1708 pointed out, it's more obvious what is going on if you place the 'const' qualifier immediately before the thing it's modifying, which is either the pointer operator or the variable name, never the type itself:
void const *const_pointer = &const_pointer;
void *const const_value = &const_value;
What would something like "const int" even mean on its own, anyway? There is no such thing as a mutable integer. It's the memory location holding the integer which may be either mutable or immutable.Edit: Actually I don't think the pointer is put anywhere, rather its value is stored in .data (non-read-only data) so it can be mutated without issue. Again though, my assembly isn't amazing.