AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   static variable - question (https://forums.alliedmods.net/showthread.php?t=189203)

Diegorkable 07-04-2012 12:03

static variable - question
 
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 }
?

Bugsy 07-04-2012 12:15

Re: static variable - question
 
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.

fysiks 07-04-2012 13:26

Re: static variable - question
 
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.

ConnorMcLeod 07-04-2012 13:34

Re: static variable - question
 
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



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

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