problem with the const variable

This is a discussion on problem with the const variable within the C Programming forums, part of the General Programming Boards category; hello guys, i am new this form. i have a problem with the following program. the main aim of this ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    181

    problem with the const variable

    hello guys, i am new this form. i have a problem with the following program. the main aim of this program is to cahnge value of the const varibale using the pointer. this program is to just demonstrate that using is pointer we are allowed to change the const varaible.

    code

    #include<stdio.h>

    int main()
    {
    const int c=10;
    int *p;

    p=(int *)&c;

    *p=20;

    printf("%d",c);
    getchar();
    }

    saved as a .c extention

    the result is success the const varibale is changes by 20

    but when i save this as .cpp extention is cant see any change with the const variable can any one tell me is that:

    here is the code which i wrote in cpp

    #include<iostream.h>

    int main()
    {
    const int c=10;
    int *p;

    p=(int *)&c;

    *p=20;

    cout<<<c;
    }

    can any one tell what is the problem here. why it is not changed in c++. and how is const variable is secures by changes the original value in the ,memory

    thanks in advance

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    In C, doing this is undefined behavior.

    Please use [code][/code] tags.
    Last edited by Dave_Sinkula; 01-28-2005 at 08:58 AM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    this program is to just demonstrate that using is pointer we are allowed to change the const varaible.
    Where did you learn this? Whatever the source, burn it, delete it, kill it, blow it up, whatever. Just get rid of this dangerous source of misinformation in a way appropriate to the medium.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 08:05 AM
  2. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 02:03 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  4. fatal error LNK1104
    By DMH in forum C++ Programming
    Replies: 2
    Last Post: 11-16-2005, 03:46 AM
  5. STL problem...
    By sagi1210 in forum C++ Programming
    Replies: 5
    Last Post: 05-03-2004, 06:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21