Re: const int value changing....
- From: Jack Klein <jackklein@xxxxxxxxxxx>
- Date: Mon, 22 Aug 2005 00:57:12 -0500
On 21 Aug 2005 22:35:26 -0700, "shyju_kambi" <n.shyju@xxxxxxxxx> wrote
in comp.lang.c:
> when a 'const int ' is declared, where is it been created(in RAM?)
What is RAM? It is not something that the C standard defines.
> #include<stdio.h>
> int main()
> {
> const int i=10;
> const int *p;
> p=&i;
> *p=12;
If your compiler accepts this code, it is very, very broken. What is
this the thing calling itself a C compiler that accepts this? This is
a constraint violation, and a conforming implementation must issue a
diagnostic.
> printf("\n %d %d",i,*p);
> return 0;
> }
>
> ---------------------------------------------------------
> the following program i tryied, still i was unable to change the value
> of a const.
If you try to change the value of a const object, you do not have a C
program anymore. You have broken the rules and left the world of C
behind. You have produced undefined behavior, and C does not know or
care what happens. Neither do this group.
> when a variable is declared, a ram location is allocated for it. then
There are so many things wrong with the sentence above that I hardly
know where to start. First of all, declaring a variable does not
allocate any storage at all, unless the declaration happens to be a
definition as well. Secondly, as I said above, C doesn't define "ram"
even when you spell it with lower case letters.
> why i cant change the value of that variable using pointer?
>
> if the variable i've declared as a 'static int ' then using pointer i
> can change the value.
>
>
> plz give solutions
There is no solution. C does not say that you can change a const
object. It does not say that you cannot change a const object. It
says that if you try to change a const object, you produce undefined
behavior. Then it is no longer C.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
.
- References:
- const int value changing....
- From: shyju_kambi
- const int value changing....
- Prev by Date: Re: What does this warning mean
- Next by Date: Re: What does this warning mean
- Previous by thread: const int value changing....
- Next by thread: Re: const int value changing....
- Index(es):
Relevant Pages
|