Hell "o" to all programmers out there.
I was just fiddling around with pointers to const when i wrote this snippet.

int main (void) 
{
  int* ptr = NULL;
  const int i = 20;
  ptr = (int*) &i;
  *ptr = 40;
  printf ("\nThe addr of the const var is %p", &i);
  printf ("\nThe addr in ptr is %p", ptr);
  printf ("\nThe value of i is %d", i);
  printf ("\nThe value of location pointed by ptr is %d",   *ptr);
  getchar ();
  return 0;
}

THe problem i am facing is that the addr of the constant variable and that present in the pointer variable turns out to be the same but still when i modify the value using pointer to constant variable it raises no error. But when i try to query the value persent in the memory location pointed by the pointer and the value in the const variable it turns out to be different ???

How come the same memory location holds two different values?
Any suggestions or explanations would be gladly accepted.

Thanks to all in advance. Bye.

So you deliberately break the type system and then wonder why it doesn't work? :) What were you expecting to happen?

Here is why it happened:
C programs live in memory as segments. The segments have
names like TEXT and BSS. Anyway, const values get placed in read-only memory, one of the segments. The read/write DATA segment is always created, and it looks like your compiler bent over backwards to allow you to write someplace, probably there.

Bottom line: you've got undefined behavior. That means there is no real explanation for the behavior you see and no expectation or guarantee that it will ever work.

In C, const does not mean constant. It means read-only (whether or not such objects are placed in read-only memory or not is up to the system). If you choose to circumvent the compiler and write to read-only memory, behavior is undefined -- as already mentioned.

So does this mean that the const qualified variables are placed in the CODE segment of the program rather than the normal DATA segment of the program like the array names?

So is this behaviour i.e. different values present at the same memory location different for different operating systems and different compiler designs ?

Thanks for your help. Bye.

So does this mean that the const qualified variables are placed in the CODE segment of the program rather than the normal DATA segment of the program like the array names?

Maybe yes, maybe no. If it's const qualified, you're not supposed to modify it.

So is this behaviour i.e. different values present at the same memory location different for different operating systems and different compiler designs ?

There are not different values at the same memory location.

[edit]You probably compiled this with a C++ compiler, where const has a different meaning and optimization may be made that would appear to confuse you.

There are not different values at the same memory location.

Maybe you are right but this is the output of a single run ;

The addr of the const var is 0012FEC8
The addr in ptr is 0012FEC8
The value of i is 20
The value of location pointed by ptr is 40

This is wat confuses me since you can note that both the addr are the same.

You probably compiled this with a C++ compiler, where const has a different meaning and optimization may be made that would appear to confuse you.

Yes you are right, i compiled this thing with a VC++ 8 compiler.

Does it make a difference if i would have compiled it usign a pure C compiler?

Does it make a difference if i would have compiled it usign a pure C compiler?

Yes, I believe so. I can't find any references off hand, but it's like the fact that you can use a const int to declare an array size in C++ because it is a true constant -- thus the compiler may not use the value at the location but instead just plug in what the const value was assigned. Whereas in C you cannot declare an array size using a const int because it is not a true constant. Something like that.

But given this:

const int i = 20;

the compiler can then take this:

printf ("\nThe value of i is %d", i);

and turn it into this:

printf ("\nThe value of i is %d", 20);

I believe.

[edit]If you got a little jiggy[*] with the code, you might produce this with a C++ compiler.

#include <stdio.h>

void foo(const int *ptr)
{
   printf ("*ptr = %d\n", *ptr);
}

int main (void) 
{
  const int i = 20;
  int* ptr = (int*) &i;
  *ptr = 40;
  printf ("   i = %d\n", i);
  printf ("*ptr = %d\n", *ptr);
  foo(&i);
  foo(ptr);
  return 0;
}

/* my output
   i = 20
*ptr = 40
*ptr = 40
*ptr = 40
*/

[*]Not a technical term. :)

Yes you are right even i get the same output.
Maybe the behaviour of the const whose value is being modified is undefined afterall, so better not walk on the broken glass of undefined behaviour.

Well to the ppl who have come to this thread to learn somthign about constants in C and C++ maybe this can be of some help :-

http://en.wikipedia.org/wiki/Const

Hope this helped. Bye.

This article has been dead for over six months. Start a new discussion instead.