2 ms·
// x is just a read-only pointer to something that may or may not be a constant void constFunc(const int *x) Is that correct? I read that as “x is a pointer
by Someone 7y ago
// x is just a read-only pointer to something that may or may not be a constant
void constFunc(const int *x)
Is that correct? I read that as “x is a pointer to a const int”
If you want “x is a read-only pointer to an int”, you would need
void constFunc(int * const x)
The article also doesn’t mention the case “x is a read-only pointer to a constant int”. To state that, you would use
void constFunc(const int * const x)
(https://stackoverflow.com/a/1143272 https://stackoverflow.com/a/1143272)
- mikeash 7y agoI believe the distinction they’re making is that there is actually no guarantee that the value pointed to by x will remain constant. All this tells you is that you’re not allowed to use x to make the modification. Consider: int foo = 42; const int *x = &foo; foo = 43;
- dmitrygr 7y agoRestrict keyword does do that if you wish to say that to the compiler. It tells it that no parameters point to the same memory. So if you won't change things via x, *x won't change.