Raised This Month: $ Target: $400
 0% 

static variable - question


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Diegorkable
Veteran Member
Join Date: Jun 2011
Old 07-04-2012 , 12:03   static variable - question
Reply With Quote #1

Hey guys.

If I declare a static variable in a function like this:

PHP Code:
function()
{
      static var[
200] = { EOS }
      var[
0] = 'H'

Will every function call after the first call at the beginning of the function var's value will be:
{ EOS, EOS, EOS......., EOS }
or
{ 'H', EOS, EOS......., EOS }
?
__________________
My Projects:

Auto-Mix (Pug): 100%

Joined the Military (a soldier now) - Inactive
Diegorkable is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 07-04-2012 , 12:15   Re: static variable - question
Reply With Quote #2

It will be the same as you left it in the previous call...so the first element will have 'H' in 0 pos.

If you're familiar with global variables, think of static being the same in functionality except it is limited to the scope of the function where it was declared.
__________________

Last edited by Bugsy; 07-04-2012 at 12:18.
Bugsy is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 07-04-2012 , 13:26   Re: static variable - question
Reply With Quote #3

This is easily tested . . .

Also, to initialize all cells of an array to a single value, you must do this:

PHP Code:
new var[200] = {value, ...} 
The method you use only sets the first cell.
__________________

Last edited by fysiks; 07-04-2012 at 13:27.
fysiks is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 07-04-2012 , 13:34   Re: static variable - question
Reply With Quote #4

See http://www.compuphase.com/pawn/Pawn_Language_Guide.pdf
Code:
• Static local declarations 

A local variable is destroyed when the execution leaves the compound block 
in which  the variable was created.         Local  variables  in a  function  only  exist 
during the run time of that function.  Each new run of the function creates 
and  initializes new  local  variables.      When  a  local  variable is  declared  with 
the keyword static rather than new, the variable remains in existence after 
the  end  of  a  function.    This  means  that  static  local  variables  provide  pri- 
vate,  permanent  storage  that  is  accessible  only  from  a  single  function  (or 
compound  block).       Like  global  variables,  static local  variables  can  only  be 
initialized with constant expressions. 

• Static global declarations 

A static global variable behaves the same as a normal global variable, except 
that  its  scope  is  restricted  to  the  file  that  the  declaration  resides  in.      To 
declare a global variable as static, replace the keyword new by  static. 

[ ... ]

• Progressive initiallers for arrays 

The ellipsis operator continues the progression of the initialisation constants 
for an array, based on the last two initialized elements.  The ellipsis operator 
(three dots, or “ ...”)  initializes the array up to its declared size. 

Examples: 

Listing:  array  initializers 

      new a[10] = { 1, ... }                // sets all ten elements to 1 
      new b[10] = { 1, 2, ... }             // b = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 
      new c[8] = { 1, 2, 40, 50, ... }      // c = 1, 2, 40, 50, 60, 70, 80, 90 
      new d[10] = { 10, 9, ... }            // d = 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
__________________
- tired and retired -

- my plugins -

Last edited by ConnorMcLeod; 07-04-2012 at 13:48.
ConnorMcLeod is offline
Reply



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

Forum Jump


All times are GMT -4. The time now is 15:12.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode