I am stuck at const pointer vs const references. Const pointer like const int* or int const*
means that pointer is for const int, and pointer itself is not const. Const type can only be assigned to const reference
const int id = 10;
printf("id=%d\t&id=%p\n", id, &id);
int const *pid = &id;
printf("pid=%p\t*pid=%d\n", pid, *pid);
// *pid = 20; // fails
*(int *)pid = 20; // works, but WHY???
printf("pid=%p\t*pid=%d\n", pid, *pid);
const int &rid = id;
printf("rid=%d\t&rid=%p\n", rid, &rid);
// rid = 30; // this fails, and afaik nothing can be done to make this writable
// int &ref2 = id; // also fails
The output will be
id=10 &id=0x7ffce84a9e14
pid=0x7ffce84a9e14 *pid=10
pid=0x7ffce84a9e14 *pid=20
rid=20 &rid=0x7ffce84a9e14
References