#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    41
    Rep Power
    12

    How to re-assigned defined variables


    Hi there,

    I have a question to ask, hope someone can help.

    I have a global variable declared, and I would like to reassign its value.

    example:

    #define MAX 10
    ......
    int limit;
    limit=100;

    something like that, any help will be appriciated!

    correction: it doesn't have to be a global variable (#define...), but it has to be able to be accessed anywhere in my code in any functions. I think there is something called static int ...., but how to use it???

    Geekoid
    Last edited by Geekoidxp; June 9th, 2003 at 01:49 PM.
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Hope I understood the question.

    When you use a #define, the preprocessor first goes through the source code and replaces that define in the source code before it even starts to try to parse anything. So:
    #define MAX 10

    int array[MAX];

    becomes:

    int array[10];

    So if you use a #define, you cannot change its value during runtime.

    In C++, they recommend using a global constant, though that wouldn't help with the array declaration:

    const int max = 10;

    But since it's a const, you won't be allowed to change it later on.

    A global variable can be initialized and then changed later:

    int max = 10;

    The thing is, if you have multiple modules, then the linker will make max available in all of the modules and you could not have max defined as a global in any of the other module (or at least it shouldn't let you).

    The way around that problem is the keyword static. If you declare a variable or a function as being static, then it is only known within that module and cannot be accessed from outside the module. If you declare max as:

    static int max = 10;

    Then you could have it declared in each and every other one of the other modules without any conflicts. But then it would only be known within that module and any changes that module made would only apply inside that module.

    But you want that variable to be accessible from any of the modules, so declaring it as static will defeat your purpose. Here's what it looks like you should do:

    1. In a header file that will be included into all the modules that need to use that global, include this line (assume your example of int max):

    extern int max;

    This tells the compiler that max is an int and that it's actual declaration is performed elsewhere. The compiler will insert into the object file information that the linker will use later to "fix" the address of int, but except for that address the compiler know everything else it needs to work with max.

    2. In every module that needs to use max, include that header file.

    3. In one of the modules, declare max for real and initialize it:

    int max = 10;

    If you forget to do this, then you will get a linker error complaining about an undefined variable.

    4. Now you can access max in any of those other modules and any of them can change its value at runtime, whereupon all the other modules can also see that changed value.

    Does that help answer your question?
  4. #3
  5. No Profile Picture
    status unknown
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    262
    Rep Power
    12
    An excellent response. Just one very minor nit-pick:
    Originally posted by dwise1_aol

    extern int max;

    This tells the compiler that max is an int and that it's actual declaration is performed elsewhere.
    extern int max *is* a declaration. The definition takes places elsewhere.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    41
    Rep Power
    12
    Yup it worked.

    Thanks dwise1_aol.

    Geekoid
  8. #5
  9. Left due to despotic ad-min
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Posts
    1,044
    Rep Power
    14
    The responses above are excellent; only one minor point overlooked: it is possible to redefine a macro

    Consider

    #define MAX 10

    #if defined(MAX)
    #undef MAX
    #define MAX 100
    #endif

    Why the #if defined(MAX) ?

    #undef doesn't work unless the thing being undefined has been previously defined, just as #define can't be used to change something that has been previously defined.

IMN logo majestic logo threadwatch logo seochat tools logo