c - What exactly does "const int *ptr=&i" mean?Why is it accepting addresses of non-constants? -
your answers sought clear major lacuna in understanding const
realized today.
in program have used statement const int *ptr=&i;
haven't used const
qualifier variable i.two things confusing me:
1) when try modify value of i using ptr
,where have used const int *ptr=&i;
,i error assignment of read-only location '*ptr'|
,even though haven't declared variable i const
qualifier.so exactly statement const int *ptr=&i;
mean , how differ int * const ptr=&i;
?
i had drilled head const int *ptr=&i;
means pointer stores address of constant,while int * const ptr=&i;
means pointer constant , can't change.but today 1 user told me in discussion(link) const int *ptr means memory pointed must treated nonmodifiable _through pointer_
.i find new kinda means "some select pointer can't alter value(while others can)".i wasn't aware of such selective declarations!!but 180k veteran attested that user correct!!.so can state in clearer,more detailed , more rigorous way?what ``const int *ptr=&i; mean?
2) told can lie program in statement const int *ptr=&i;
assigning address of non-constant pointer.what mean?why allowed that?why don't warning if assign address of non-constant pointer ptr
expects address of constant?and if forgiving being assigned address of non-constant,why throws error when try change value of non-constant,which reasonable thing do,the pointed variable being non-constant?
#include <stdio.h> int main () { int i=8; const int *ptr=&i; *ptr=9; printf("%d",*ptr); }
error: assignment of read-only location '*ptr'|
the definition const int *ptr = &i;
says “i not modify i
through ptr
.” not int
ptr
points const
, says ptr
should not used modify it.
this conversion allowed because makes sense: since i
not const
, allowed modify it, can choose not modify it. no rule broken when choose not modify i
. and, if create pointer i
, “i not going use pointer modify i
”, fine too.
the reason want can pass address of object routine takes pointer const
object. example, consider strlen
routine. strlen
routine not modify input, parameter pointer const char
. now, have pointer char
, , want know length. call strlen
. passing pointer char
argument parameter pointer const char
. want conversion work. makes complete sense: i can modify char
if want, strlen
routine not going to, treats them const char
.
1) error *ptr = something;
because declared ptr
pointer const int
, violated promise not use ptr
modify int
. fact ptr
points object not const
not negate promise not use ptr
modify it.
2) not lie assign address of non-const
object pointer const
. assignment not object const
, says pointer should not used modify object.
additionally, although have not asked this, const
attribute in c not binding. if define object const
, should not modify it. however, there other situations in pointer const
passed routine converts pointer non-const
. defect in language, inability retain information necessary handle const
in ways might prefer. example strchr
routine. declaration char *strchr(const char *s, int c)
. parameter s
const char *
because strchr
not change input. however, pointer strchr
routines derived s
(it points 1 of characters in string). so, internally, strchr
has converted const char *
char *
.
this allows write code passes char *
strchr
, uses returned pointer modify string, fine. means compiler cannot protect mistakes such passing const char *
strchr
, using returned pointer try modify string, may error. shortcoming in c.
Comments
Post a Comment