<pre>
#include <stdio.h> void function ( int const * const value ) { printf ("%d\n", * value); / * more code * / ( * value)++; / * more code * / ++value; }
int main(int argc, char * * argv) { int value = 1; int * pvalue = &value; function(pvalue); return 0; }
$ gcc t1.c t1.c: In function ‘function’: t1.c:5: error: increment of read-only location ‘* value’ t1.c:7: error: increment of read-only location ‘value’ </pre>
quick googling: http://www.cprogramming.com/tutorial/const_correctness.html
EDIT: how can we properly paste code in here? The * mess things up.
#include <stdio.h>
void function(int const * const value)
{
printf ("%d\n", *value);
/* more code */
(*value)++;
/* more code */
++value;
}
int main(int argc, char **argv)
{
int value = 1;
int *pvalue = &value;
function(pvalue);
return 0;
}
$gcc t1.c
t1.c: In function ‘function’:
t1.c:9: error: increment of read-only location
t1.c:13: error: increment of read-only location
BTW: I agree, const is really usefulEDIT: I've recompiled on OSX to correct the error message with the correct lines.
void function(int const * const value)
it worries me because it shows me that the writer doesn't know the meaning of const. There is no added value of this declaration over void function(int const *value)
or the more commonly seen void function(const int *value)
The only thing that has changed in your code is that the implementor of the function can't modify his own private copy of the pointer.I admittedly don't use C++ that frequently, but if I make my code const-correct it always seems to cause a lot of false positives, while seldom finding actual bugs.
I've never found a need for it. I usually don't care if the pointer itself is modified, particularly since such modifications are usually local and obvious.