Reputation: 77
I have written this piece of code
#include <stdio.h>
int main() {
int b = 15;
const int *foo = &b;
//b = 20;
*foo = 20;
printf("%d", *foo);
return 0;
}
This means that the location that foo
is pointing to cannot change. That means b
cannot change . But when I uncomment the line b = 20
. It doesn't show any error and I get the output 20
. While in this piece of code I get this error
main.c: In function 'main':
main.c:15:10: error: assignment of read-only location '*foo'
*foo = 20;
If *foo
that is b
is read only location, why can I change its value b = 20
?
Upvotes: 3
Views: 140
Reputation: 77
The answer to this question is after declaring the pointer as
const int *foo = &b;
you cant use *foo as a L-value because this code also gives the same error
#include <stdio.h>
int main() {
int a=10,b = 15;
const int *foo = &b;
foo=&a; //assigning foo the address of a
*foo=30; // Again here you cant use *foo
printf("%d", *foo);
return 0;
}
Upvotes: 0
Reputation: 106012
What does
int b=15;
const int* foo=&b;
mean?
It means,
b
is a modifiable int
object. &b
has type int *
. The declaration for foo
converts &b
to type const int *
. This is a valid qualification conversion. int
object pointed by foo
must not be modified using foo
.The above qualification conversion does not invalidates the declaration of b
, i.e. it still is modifiable. b
can still be used to update its value but *foo
can't be used.
Think const
as a promise. You promised the compiler that you will not change the value at the location pointed by foo
, but no one is stopping you to break that promise by changing that value at that location by other means.
Upvotes: 8
Reputation: 35154
I think chqrlie is right when he says that the error message "assignment of read-only location '*foo'" is misleading. Actually, const int *foo
(i.e. a pointer defined as pointing to a const int
) does not mean that the memory addressed by the pointer is or becomes immutable per se; it just means that the memory addressed by the pointer must not be changed through this pointer; it might, however, be changed in other ways. You could define, for example a second pointer int *foo2 = &b
through which you may change the value addressed by foo2
.
So you may think of the pointer as being a separate view to a value at a particular memory address, and one may declare the view as read-only or not. This does, however, not effect whether the value at the particular memory address per se is immutable or not:
int main()
{
int b=15; // b is mutable
const int* foo=&b; // *foo is immutable; b remains mutable
int *foo2=&b; // *foo2 is mutable
b=20; // allowed, since b is mutable
*foo=20; // not allowed, since "*foo" is immutable, even if the value to which it points (i.e. b) would be mutable
*foo2=20; // allowed, since "*foo2" is mutable
return 0;
}
Though not asked, the other way round, you actually may define a value as being immutable, and this value must then not be altered in any way; if you somehow manipulated the value by writing to its memory address, the behaviour is undefined:
const int c = 15; // c is immutable
int* bar = &c; // warning: discards const qualifier
printf("%d\n", c);
*bar = 20; // undefined behaviour from here on; modifying a const value is not allowed.
printf("%d\n", c); // undefined behaviour; good chance that it still prints 15, because a compiler might have relied on the fact that c is 15 and immutable
printf("%d\n", *bar); // undefined behaviour; probably printing 20?
Hope it helps.
Upvotes: 7
Reputation: 6467
No, const int* foo = &b;
doesnt mean you cant change b
, it means that you cant dereference this pointer and change value of dereferenced pointer like this
*foo = 20;
You cant say with const
that some piece of memory is unmodificable for all. You can say that only that const
variable cant change this piece of memory, so this operation (if b
hasnt const
modifier) is perfectly valid even if you have const pointer pointing to b
.
b=20;
Upvotes: 3
Reputation: 67546
If you want a constant pointer to not constant object declare it:
T * const ptr = &t
Where T
is the type of the object and t
is the object itself
Upvotes: 0