views:

231

answers:

9

Possible Duplicate:
constants and pointers in C

#include <stdio.h>
int main()
{
    const int a = 12;
    int *p;
    p = &a;
    *p = 70;
}

Will it work?

+3  A: 

It does indeed work with gcc. It didn't like it though:

test.c:6: warning: assignment discards qualifiers from pointer target type

But the value did change when executed. I won't point out the obvious no-no...

Alexander Sagen
A: 

NO we cannot change the value constant variable.this loses the integrity of the language. const variable are meant to be constant.BTW,did you test it? what is the out put?

Vijay Sarathi
it works it changes the value of constant a to 70. A really unexpected result
Shweta
The compiler turned me into a cat. A really unexpected result. But legal according to the C specification.
jleedev
`const` variables are not necessarily constant, just not changeable by your code.
detly
+2  A: 

It's "undefined behavior," meaning that based on the standard you can't predict what will happen when you try this. It may do different things depending on the particular machine, compiler, and state of the program.

In this case, what will most often happen is that the answer will be "yes." A variable, const or not, is just a location in memory, and you can break the rules of constness and simply overwrite it. (Of course this will cause a severe bug if some other part of the program is depending on its const data being constant!)

However in some cases -- most typically for const static data -- the compiler may put such variables in a read-only region of memory. MSVC, for example, usually puts const static ints in .text segment of the executable, which means that the operating system will throw a protection fault if you try to write to it, and the program will crash.

In some other combination of compiler and machine, something entirely different may happen. The one thing you can predict for sure is that this pattern will annoy whoever has to read your code.

Crashworks
+1  A: 

You cannot change the value of a constant variable by using a pointer pointing to it. This type of pointer is called as Pointer to a constant.

There is also another concept called Constant Pointer. It means that once a pointer points to a memory location you cannot make it point to the another location.

Abhijeet Pathak
but the value changed when I run this code
Shweta
+6  A: 

Modifying a const qualified object through a pointer invokes undefined behaviour, and such is the result. It is likely to be something you'd expect though, ie. the previous value unchanged, if it has been placed in .text, etc.

Michael Foukarakis
Another possible result is "some expressions behave as if `a` is still 12, while others behave as if `a` is now 70", which may or may not be "something you'd expect".
caf
Hence, "undefined".
Michael Foukarakis
+2  A: 

yes, you can make it done by using such code. but the code do not apply when when a is global (a gcc-compiled program gave me segmentation fault.)

generally speaking, in beloved C, you can almost always find someway to hack things that are not supposed to be changed or exposed. const here being a example.

But thinking about the poor guy(maybe myself after 6 months) maintains our code, I often choose not do so.

jokester
+1  A: 

Here the type of pointer p is int*, which is being assigned the value of type const int* (&a => address of a const int variable).

Implicit cast eliminates the constness, though gcc throws a warning (please note this largely depends on the implementation).

Since the pointer is not declared as a const, value can be changed using such pointer.

if the pointer would be declared as const int* p = &a, you won't be able to do *p = 70.

Hemant
The implicit conversion here is *not* part of the C language - a strict C compiler is allowed to reject this code entirely as an error. It is GCC's permissiveness that allows it, not the language.
caf
Thanks for pointing it out... updated the answer.
Hemant
+2  A: 

It's undefined behaviour. Proof:

/* program.c */

int main()
{
        const int a = 12;
        int* p;
        p = &a;
        *p = 70;
        printf("%d\n", a);
        return 0;
}


gcc program.c

and run it. Output will be 70 (gcc 4.3)

Then compile it like this:

gcc -O2 program.c

and run it. The output will be 12. When it does optimisation, the compiler presumably loads 12 into a register and doesn't bother to load it again when it needs to access a for the printf because it "knows" that a can't change.

JeremyP
Funny, when I run this program, the output is 42.
abelenky
@abelenky: yes but did you stop to think about what the question is? Maybe you should build a computer to find out.
JeremyP
Anyhow we can say that the value gets changed lol
fahad
A: 

Bad, BAD idea.

Also, the behavior is platform- and implementation-specific. If you're running on a platform where the constant is stored in non-writable memory, this obviously won't work.

And, why on earth would you want to? Either update the constant in your source, or make it a variable.

David Lively