Thread: Const qualifier

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    53

    Const qualifier

    From the definition of const qualifier, that the variable value cannot be changed in the current file, but can be changed in another file, I tried to write the program but resulted in segmentation fault.

    file1.c
    Code:
    #include<stdio.h>
    const int x = 5;
    void main(){
          printf("%d",x);  
          show();
    }
    file2.c
    Code:
    void show()
    {
         extern int x;
         x++;
         printf("%d",x);
    }
    Please help me..

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Honesty is the best policy.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    53
    Please tell me what is wrong in this....

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    The variable is declared as const. It's not allowed to modify it under any circumstance.

    Perhaps you're trying to come up with an example where a value is allowed to be modified in one function but is read-only in another?
    For that you could have a non-const global variable, and one function that just happily assigns to it, and another that takes a pointer to const which is passed the address of variable.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM