Q's of: static, variable, and return functions
#3

Can you please explain me some things that I couldn't understand by reading in Wiki, just clear it out.

Please tell me how will 'local static' print this, I don't understand, it says on the wiki that it doesn't forget it's old value, but I still can't understand please explain to me. It should rather print 111 because, below j is set to 1 in the loop:
Quote:
Originally Posted by wiki.sa-mp.com
static local

A static local can be used in the same place as a local but doesn't forget it's old value, for example:

MyFunc()
{
static
var1 = 4;
printf("%d", var1);
{
// var1 still exists as this is a lower level
static
var2 = 8;
printf("%d %d", var1, var2);
}
// var2 no longer exists as this is a higher level
}
// var1 no longer exists

That code will behave exactly the same as the new example, however this:

for (new i = 0; i < 3; i++)
{
static
j = 1;
printf("%d", j);
j++;
}

Will print:

1
2
3

Because j is static so remembers its old value.


I also don't understand what does wiki mean in 'global static'. I don't understand what they mean by "they can only be used in the file they are declared in":
Quote:
Originally Posted by wiki.sa-mp.com
global static

Global static variables are like normal globals but can only be used in the file in which they are declared:

File1:

static
gsMyVar = 4;

MyFunc()
{
printf("%d", gsMyVar);
}

#include "File2"

File2:

MyFunc2()
{
// This is wrong as gsMyVar doesn't exist here
printf("%d", gsMyVar);
}

static can also be applied to functions in the same way.

Can someone also explain to me what does return 0 and return 1 have in difference. Please explain me this.
Reply


Messages In This Thread
Q's of: static, variable, and return functions - by ||123|| - 22.07.2011, 10:25
Re: Q's of: static, variable, and return functions - by MadeMan - 22.07.2011, 10:45
Re: Q's of: static, variable, and return functions - by ||123|| - 22.07.2011, 11:35
Re: Q's of: static, variable, and return functions - by Double-O-Seven - 22.07.2011, 11:42
Re: Q's of: static, variable, and return functions - by Grim_ - 22.07.2011, 11:46
Re: Q's of: static, variable, and return functions - by MadeMan - 22.07.2011, 11:53
Re: Q's of: static, variable, and return functions - by ||123|| - 22.07.2011, 12:01
Re: Q's of: static, variable, and return functions - by Roko_foko - 22.07.2011, 12:16

Forum Jump:


Users browsing this thread: 4 Guest(s)