Re: const int value changing....



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
.



Relevant Pages

  • Re: const int value changing....
    ... > when a 'const int ' is declared, where is it been created(in RAM?) ... the compiler won't allow you to modify a const variable. ...
    (comp.lang.c)
  • Re: Volatile const
    ... Standard that defines the C language, ... the const qualifier, so it is not really "variable" in that sense. ... The answer, which is what I think you mean by "Ans" is not ROM or RAM, ... not be modified through that pointer. ...
    (comp.std.c)
  • Re: Is this legal?
    ... That it is the only way to initialise an 'const' ... | online Comeau snippet compiler. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: final parameters
    ... make sure that, if applicable, all the parameters were declared const. ... This difference is primarily from the fact that java passes everything ... actually recall reading something to that effect (declaring everything ... public void test(final int val1, ...
    (comp.lang.java.programmer)
  • Re: Larkin, Power BASIC cannot be THAT good:
    ... part of "const int" and therefore actually allocated memory and stored the value, fetching it again every time it was needed. ... A lot of the older compilers did that copy to ram even when they purported to generate code for embedded systems. ... And you can be sure that no attempt by the CPU to trash a ROM value will ever succeed. ... You could tell it was in big trouble if the user register bank was in ROM where incrementing the program counter doesn't work any more. ...
    (sci.electronics.design)