C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 9th, 2003, 12:22 PM
Geekoidxp Geekoidxp is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 41 Geekoidxp User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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.

Reply With Quote
  #2  
Old June 9th, 2003, 03:44 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,867 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 2 h 46 m 57 sec
Reputation Power: 480
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?

Reply With Quote
  #3  
Old June 9th, 2003, 05:24 PM
BigBadBob BigBadBob is offline
status unknown
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 262 BigBadBob User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
An excellent response. Just one very minor nit-pick:
Quote:
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.

Reply With Quote
  #4  
Old June 10th, 2003, 01:27 AM
Geekoidxp Geekoidxp is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 41 Geekoidxp User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Yup it worked.

Thanks dwise1_aol.

Geekoid

Reply With Quote
  #5  
Old June 14th, 2003, 07:37 AM
grumpy's Avatar
grumpy grumpy is offline
Left due to despotic ad-min
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2003
Posts: 1,042 grumpy User rank is Corporal (100 - 500 Reputation Level)grumpy User rank is Corporal (100 - 500 Reputation Level)grumpy User rank is Corporal (100 - 500 Reputation Level)grumpy User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 53 m 47 sec
Reputation Power: 8
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > How to re-assigned defined variables


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT