Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > referencing a global variable from a function without passing it asan argument

Reply
Thread Tools

referencing a global variable from a function without passing it asan argument

 
 
iC and iC++
Guest
Posts: n/a
 
      11-22-2010

I have the following code snippet from my program that i am working on

int running = 0;
float var1, average_value;

void is_process_running(void) {
if (fabs(var1 - average_value) <= 0.001)
{
*(&running) = 0;
}
else
*(&running) = 1;
}

var1 and average value get their value from somewhere else.

is it possible to change the value of "is_running" by referencing to
it with a pointer to its address i.e. <return *(&running)> with out
passing it as an argument to the function or making it the return
value of the function.

Thanks,
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      11-22-2010
"iC and iC++" <(E-Mail Removed)> writes:
> I have the following code snippet from my program that i am working on
>
> int running = 0;
> float var1, average_value;
>
> void is_process_running(void) {
> if (fabs(var1 - average_value) <= 0.001)
> {
> *(&running) = 0;


This is equivalent to
running = 0;
Why not just write that?

> }
> else
> *(&running) = 1;


And this is equivalent to
running = 1;

> }


> var1 and average value get their value from somewhere else.
>
> is it possible to change the value of "is_running" by referencing to
> it with a pointer to its address i.e. <return *(&running)> with out
> passing it as an argument to the function or making it the return
> value of the function.


Assuming that you mean "running" rather than "is_running", yes, of
course it's possible; that's what your code snippet does. (But it's a
pointer to the object, not "a pointer to its address". A pointer value
*is* an address.)

Excessive use of globals variables is a bad idea. Without commenting on
the references to var1 and average_value, your is_process_running()
function should probably just return a value indicating whether the
process is running or not (that's certainly what I'd expect given the
name). For example:

int is_process_running(void) {
return fabs(var1 - average_value) > 0.001;
}

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
 
iC and iC++
Guest
Posts: n/a
 
      11-22-2010

> * * int is_process_running(void) {
> * * * * return fabs(var1 - average_value) > 0.001;
> * * }
>


Thanks a lot. This is great. I am trying to limit the use of global
variables as much as I can.
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      11-23-2010
On Mon, 22 Nov 2010 12:37:39 -0800 (PST), "iC and iC++"
<(E-Mail Removed)> wrote:

>
>> * * int is_process_running(void) {
>> * * * * return fabs(var1 - average_value) > 0.001;
>> * * }
>>

>
>Thanks a lot. This is great. I am trying to limit the use of global
>variables as much as I can.


Then the values to be processed by the function should be passed as
arguments rather than store them in global variables referenced by the
function directly.

--
Remove del for email
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Variable argument function as a parameter of a variable argument function AikidoGuy C Programming 11 11-21-2011 10:43 PM
In python CGI, how to pass "hello" back to a javascript function asan argument at client side? zxo102 Python 2 10-13-2009 09:05 AM
Passing a function as an argument and using the evaluated functionas an argument User1014 Javascript 1 11-30-2006 12:13 PM
Function pointers, variable argument functions calling other variable-argument functions (sort of) S?ren Gammelmark C Programming 1 01-07-2005 09:41 PM
How to pass variable argument list to another function w/ variable argument list? Ben Kial C Programming 1 11-15-2004 01:51 AM



Advertisments