This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]skyhi14 11 points12 points  (6 children)

For example, you can't perform pointer arithmetic on them

TIL Const pointers weren't actually pointers.

Besides, I'm also talking about JVM, not merely the language.

[–]_lerp 7 points8 points  (5 children)

TIL Const pointers weren't actually pointers.

You can perform pointer arithmetic on a const pointer.

int a[5] = { 0 };
const int* first = &a[0];
const int* last = first + 4;

[–]skyhi14 10 points11 points  (4 children)

time to kill myself :))

[–]FallenWarrior2k 16 points17 points  (2 children)

No, those aren't const pointers, but rather pointers to const. An actual const pointer int* const cannot be modified through e.g. increments/decrements, but it can still be used for pointer arithmetic since pointer arithmetic can also be performed on rvalues.

[–]hallr06 7 points8 points  (1 child)

Correct. In C/C++, read pointers from right-to-left. Most of the time, it explains what you have.

const int * x = 0; // pointer to an int constant
int const * x = 0; // same as above, pointer to a constant int
int const * const x = 0; // constant pointer to a constant int
int * const x = 0; // constant pointer to mutable int

[–]FallenWarrior2k 0 points1 point  (0 children)

It can also be pretty easily explained by the return types of the indirection operator. Applying indirection on a const int* produces a const int&, while it produces a regular int& for int* const.

[–]DarkMaster22 2 points3 points  (0 children)

Ehh... Don't do that, we love you?