GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 15-Oct-2007, 21:08
ahbi82 ahbi82 is offline
Member
 
Join Date: Jul 2006
Posts: 115
ahbi82 will become famous soon enough

Life Time of Function static variable?


I would like to discuss something about a static variable in a function body.

CPP / C++ / C Code:
static void testS( int* var )
{
   static int out = 10;

   var = &out;
}

int main( void )
{
   int* testVar = NULL;

   testS( testVar );

   return 0;
}


Ok here's my question. Since static variable is valid through the whole life time of the program in the function body. Since i reference the static variable through the function call, can i change the value of the static variable outside the function body??
  #2  
Old 15-Oct-2007, 23:19
seprich seprich is offline
Member
 
Join Date: Jun 2007
Posts: 110
seprich has a spectacular aura aboutseprich has a spectacular aura about

Re: Life Time of Function static variable?


Quote:
Originally Posted by ahbi82
Ok here's my question. Since static variable is valid through the whole life time of the program in the function body. Since i reference the static variable through the function call, can i change the value of the static variable outside the function body??
You cannot normally access static variable declared inside the function from outside the function. However c allows playing with pointers quite freely and since static variable stays in memory it is possible to have pointer to it from anywhere and to modify it. But then that kind of style is not good programming style. You should declare the variable global if you intend to modify it outside the function.

Very bad style, but shows that it can be done:
CPP / C++ / C Code:
#include <stdio.h>

void not_good( int **var )
{
    static int foo = 10;
    *var = &foo;
}
int main()
{
    int *m;

    not_good( &m );
    printf( "static in foo is now %d \n", *m );
    (*m)++;
    printf( "and now it is %d \n", *m );
}
but really you should use global variable.
  #3  
Old 16-Oct-2007, 00:30
ahbi82 ahbi82 is offline
Member
 
Join Date: Jul 2006
Posts: 115
ahbi82 will become famous soon enough

Re: Life Time of Function static variable?


Quote:

But then that kind of style is not good programming style. You should declare the variable global if you intend to modify it outside the function.

When i does programming, i'll try not to use globals if i can as its also a good programming practice. From my point of view, it depends on applications that you are developing. Don't worry, this is just a discussion not a argument or what. Just to share some of my findings........

  #4  
Old 16-Oct-2007, 01:24
seprich seprich is offline
Member
 
Join Date: Jun 2007
Posts: 110
seprich has a spectacular aura aboutseprich has a spectacular aura about

Re: Life Time of Function static variable?


btw. in my previous example I forgot to write the "return 0;" to the main function.

anyways .. using globals is not always a bad idea. It can be very good indeed for storing information which you will need in many functions.
When some newbie programmers tend to declare everything as a global variable regardless of where the variable is going to be needed then yes we give the advice that try to avoid global variables. The declaration place of the variable determines the scope of the variable and when you plan/write your program you must invest some tought to the question "where am i going to use and access this information". Answer to that question gives you the scope where to put the variable declaration
 
 

Recent GIDBlogWriting a book by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
triangle (polygon), drawing, sizing, and rotation programme using linked lists... promsan C Programming Language 12 14-May-2007 14:03
constructors/classes mapes479 C++ Forum 3 19-Nov-2006 17:34
Handling keyup events. harroldm FLTK Forum 3 24-Aug-2006 08:48
Revising Script style ?????? pepee MySQL / PHP Forum 4 14-Apr-2004 04:59

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:44.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.