Is it possible to have a stand-alone static variable within a function? [duplicate] - static

This question already has answers here:
Static function variables in Swift
(4 answers)
Closed 8 years ago.
How can I have a static variable within a function as shown below?
func addItem(petItem:Item) -> Bool {
var static count = 0 //... pseudo code
...
...Or must I have all static codes at the top of the file (global area)?

Or must I have all static codes at the top of the file (global area)?
Yep, that. You need to declare your variable in the scope where it will persist. Exactly where that should be depends on what you are trying to achieve.

Related

What do we earn by making global variables as non-global variables by using static keyword? [duplicate]

This question already has answers here:
Why declare a variable or function static in C?
(4 answers)
Closed 3 years ago.
I have learned that those global variables that will not be used outside the file should be made non-global variable by appending static keyword like static int a;
I want to know that why we should do this?
Static implies a translation unit level scope only.
However, you don't need to make it non-global + static.
You can very well make it global static to curb the scope to translation unit level.

How to initialize a static non-primitive type in Rust? [duplicate]

This question already has answers here:
How do I declare an instance of one of my Rust structs as static? [duplicate]
(1 answer)
How can you make a safe static singleton in Rust?
(3 answers)
How do I create a global, mutable singleton?
(7 answers)
Closed 4 years ago.
I need to have a static std::string::String . I tried initializing it in several ways but in vain. One of them being -
static mut DURATION_HEX:String = "0".to_string();
I get a note saying :
note: a limited form of compile-time function evaluation is available
on a nightly compiler via const fn
I am not able to initialize it. I cannot leave it without initialization as well.
How do I create a static variable of non-primitive type in rust?

Use of static keyword to global variable in c [duplicate]

This question already has answers here:
What does "static" mean in C?
(20 answers)
Closed 5 years ago.
If I apply static keyword to local variable, its value is maintained within function invocations. However, if I apply static keyword to global variable, will it be as good as declaring the variable as global? What I mean to say is does static keyword has any effect on global variables?
It means that variable has file scope. You cannot access that global variable (by name) from another file.

What is the meaning of 'static void *' in C? [duplicate]

This question already has answers here:
What does "static" mean in C?
(20 answers)
Closed 6 years ago.
I'm new to C and recently when i'm learning zeromq (work related) I'm kinda confused with the static void *:
...
static void *
worker_task(void *args)
{
...
What is the exact the meaning of that line? I tried looking for answer, I thought that it was a pointer but It is kinda odd, given that pointer usually have a variable name after the '*'.
Thank you very much, I hope It's not rude of me for asking this seemingly "novice" question. :)
The function worker_task returns a void *.
The static keyword in front of the function definition means that the function is only viewable inside of the current compilation unit, i.e. a given object file, typically built from one source file with several include files. The function is not visible from other object files that may be linked with with one containing this function.

Can I invoke a static function from another compilation unit by using its address? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Static function access in other files
IIRC, a static function is not visible outside of own "compilation unit", which I think is a .C file.
Can I pass its address as a parameter to a function in another unit so that the second unit will later invoke the first unit's static function as a callback?
I am guessing that the "visibility" of the static function is its visibility to the linker, so that, while I cannot directly invoke a static function of unit1.c in unit2.c, I can pass its address and invoke it by address.
Can anyone confirm that? Sorry, my C is a bit rusty these days. Thanks in advance for any help.
Yes, you can call static function in that way

Resources