const int value changing....

S

shyju_kambi

when a 'const int ' is declared, where is it been created(in RAM?)

#include<stdio.h>
int main()
{
const int i=10;
const int *p;
p=&i;
*p=12;
printf("\n %d %d",i,*p);
return 0;
}

---------------------------------------------------------
the following program i tryied, still i was unable to change the value
of a const.

when a variable is declared, a ram location is allocated for it. then
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
 
J

Jack Klein

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;
}

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.
 
M

Michael Mair

shyju_kambi said:
when a 'const int ' is declared, where is it been created(in RAM?)

C has nothing to say about it.
You declare a const int; this means you promise you are not going
to modify it. Full stop.

Some implementations may take all the constant stuff and enable
you to burn it into some ROM, others may have the opportunity to
store it on memory pages marked read-only. Others will just store
it along with the rest.
#include<stdio.h>
int main()
{
const int i=10;
const int *p;
p=&i;

Your compiler needs to issue a warnign here.

You are invoking undefined behaviour. Everything following from
here need not surprise you.
printf("\n %d %d",i,*p);
return 0;
}

---------------------------------------------------------
the following program i tryied, still i was unable to change the value
of a const.
Great!

when a variable is declared, a ram location is allocated for it. then
why i cant change the value of that variable using pointer?

You are not allowed to and your implementation is in a position
to ensure that you do not do it.
Casting away const can only work if the original object was not
const to start with.
if the variable i've declared as a 'static int ' then using pointer i
can change the value.

Because neither static storage duration nor internal linkage say
something about modifiability of an object during its lifetime.

Postleitzahl?
Honestly, please be so polite to write proper English.
give solutions

You wanted an answer to a question.
A solution is not to use const when you do not mean it.


-Michael
 
K

Keith Thompson

shyju_kambi said:
when a 'const int ' is declared, where is it been created(in RAM?)

#include<stdio.h>
int main()
{
const int i=10;
const int *p;
p=&i;
*p=12;
printf("\n %d %d",i,*p);
return 0;
}

---------------------------------------------------------
the following program i tryied, still i was unable to change the value
of a const.

when a variable is declared, a ram location is allocated for it. then
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

Solutions to what?

If you want to modify an object, don't declare it const. If you
declare an object const, don't try to modify it.
 
C

Christian Bau

"shyju_kambi said:
when a 'const int ' is declared, where is it been created(in RAM?)

#include<stdio.h>
int main()
{
const int i=10;
const int *p;
p=&i;
*p=12;
printf("\n %d %d",i,*p);
return 0;
}

---------------------------------------------------------
the following program i tryied, still i was unable to change the value
of a const.

when a variable is declared, a ram location is allocated for it.

It seems you have a little knowledge how computers work. A little
knowledge can be very very dangerous. Forget about "ram locations".
Download a copy of the C Standard, or at least a copy of the last draft
of the C Standard, and use that as a reference. You won't find "ram
locations" mentioned there anywhere.
then
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

Solution? Don't do it.

In many cases, the compiler won't allow you to modify a const variable.
However, you can trick the compiler into allowing code that attempts to
modify such a const variable.

Any code that attempts to modify a const variable invokes UNDEFINED
BEHAVIOR. That means: Anything could happen. And that means _absolutely
anything_. If you modify a const variable, and as a result your harddisk
gets formatted, don't complain, it is your own fault. If it makes a
server crash and half a dozen people need a week to fix it, don't
complain, it is your fault.

(This will most likely not happen in a simple program that you write to
try out things. It will happen when you write a program that is used by
ten thousand customers, and then you are in trouble. )
 
M

Mabden

If you want to modify an object, don't declare it const. If you
declare an object const, don't try to modify it.

Nice, but for extra points, could you make it more Zen-like?

I'm thinking something like:

Solid meets fluid. Definition enhances knowledge.
 
K

Keith Thompson

Mabden said:
Nice, but for extra points, could you make it more Zen-like?

I'm thinking something like:

Solid meets fluid. Definition enhances knowledge.

Let us know when you have something useful to add to the discussion.
 
M

Mabden

Keith Thompson said:
Let us know when you have something useful to add to the discussion.

Solid meets fluid: A constant (solid) should not be changed (fluid).

Definition enhances knowledge: The declaration (definition) of the
object lets one know (enhances knowledge) about how the object should be
used.

Since it was merely a restatement of your own words, I thought you of
all people could find it "useful to the discussion". And others, those
with a sense of humor or familiarity with other cultures, might be
slightly amused.

For more extra points, you could also try using Haiku:

Modify object
But not if it is constant
That would be so wrong


Let us know when you have taken your Midol.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,458
Latest member
Chris#

Latest Threads

Top