|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 12:49 PM. |
|
#2
|
||||
|
||||
|
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? |
|
#3
|
|||
|
|||
|
An excellent response. Just one very minor nit-pick:
Quote:
|
|
#4
|
|||
|
|||
|
Yup it worked.
Thanks dwise1_aol. Geekoid |
|
#5
|
||||
|
||||
|
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. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > How to re-assigned defined variables |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|