local-variables

How to change local static variable value from outside function

北城余情 提交于 2019-12-02 12:07:44
#include <stdio.h> void func() { static int x = 0; // x is initialized only once across three calls of // func() printf("%d\n", x); // outputs the value of x x = x + 1; } int main(int argc, char * const argv[]) { func(); // prints 0 func(); // prints 1 func(); // prints 2 // Here I want to reinitialize x value to 0, how to do that ? <-- this line return 0; } In the above code, after calling func() 3 times I want to re-initialize x to zero. Is there any method to reinitialize it to 0? Do you want the function always to reset the counter after three invocations? or do you want to the caller to

Returning an address of local variable behaviour [duplicate]

狂风中的少年 提交于 2019-12-02 11:27:12
Possible Duplicate: Can a local variable's memory be accessed outside its scope? input: #include <stdlib.h> #include <stdio.h> int func2(void); int* func1(void); int func2(void) { int* b; b = func1(); printf("%d", *b); printf("%d", *b); printf("%d", *b); } int* func1() { int a = 13; return &a; } int main() { func2(); } Output: 13 -1077824828 -1077824828 Can someone explain what happened in the stack and OS? Why the result changed from 13 to garbage after getting the value of the pointer? Sure. The result will differ between debug and release (clean). A local variable is EBP-(some offset) if

Can a local variable be used out of a method?

你。 提交于 2019-12-02 10:56:13
I've got stuck in a problem about local variables. The following is not my original code but I use a simple example to present my question: import java.util.Scanner; public static void main(String[] args) { Scanner userScan=new Scanner(System.in); do{ int input1=userScan.nextInt(); }while(input1>10); } My purpose is to let a user type in an integer which is in my intended range. If the typed number does not meet the rule, I hope the user can type again until it does. However, the "input1" is a local variable, so it will not be valid in the expression of while. But I do not want the user to re

batch script variable unset in for loop has no effect

懵懂的女人 提交于 2019-12-02 08:03:14
问题 Below is my script. I am trying to look into folders one level below and pick out only those folders, hence the ~-9 which extracts the last 9 chars from the path. But the set var= does not unset the variable because the output comes back with the same folder name repeated # times. Also batch doesn't allow me to do this extract trick directly on %%i, hence the need for the local variable. How do I clear this variable so that it takes the new value in the next iteration? @echo off for /d %%i in

Using local declared variables in a different method in Java

心不动则不痛 提交于 2019-12-02 07:01:47
问题 I am having a little difficulty with a school assignment, long story short I declared two local variables in a method and I need to access those variables outside the method : public String convertHeightToFeetInches(String input){ int height = Integer.parseInt(input); int resultFeet = height / IN_PER_FOOT; int resultInches = height % IN_PER_FOOT; Math.floor(resultInches); return input; } I would have to print the following string in a different method : System.out.println("Height: " +

batch script variable unset in for loop has no effect

断了今生、忘了曾经 提交于 2019-12-02 06:14:15
Below is my script. I am trying to look into folders one level below and pick out only those folders, hence the ~-9 which extracts the last 9 chars from the path. But the set var= does not unset the variable because the output comes back with the same folder name repeated # times. Also batch doesn't allow me to do this extract trick directly on %%i, hence the need for the local variable. How do I clear this variable so that it takes the new value in the next iteration? @echo off for /d %%i in (%1\*) do ( set var=%%i echo %var:~-9% set "var=" ) http://judago.webs.com/variablecatches.htm has an

Setting local variables to a function instead of using globals optimize the function

左心房为你撑大大i 提交于 2019-12-01 19:32:51
In the documentation for the itertools module I found this comment def dotproduct(vec1, vec2): return sum(imap(operator.mul, vec1, vec2)) Note, many of the above recipes can be optimized by replacing global lookups with local variables defined as default values. For example, the dotproduct recipe can be written as: def dotproduct(vec1, vec2, sum=sum, imap=imap, mul=operator.mul): return sum(imap(mul, vec1, vec2)) How is it?. Is there a practical noticeable speed-up (that could balance the inconvenience of the larger function signature)? In which specific conditions the use of local variables

Passing local variable with name of a global variable isn't possible in JS?

别来无恙 提交于 2019-12-01 18:37:20
foo = "foobar"; var bar = function(){ var foo = foo || ""; return foo; } bar();` This code gives a result empty string. Why cannot JS reassign a local variable with same name as a global variable? In other programming languages the expected result is of course "foobar", why does JS behave like that? That's because you declared a local variable with the same name - and it masks the global variable. So when you write foo you refer to the local variable. That's true even if you write it before the declaration of that local variable, variables in JavaScript are function-scoped. However, you can

Scope of final local variable in java

戏子无情 提交于 2019-12-01 17:20:56
Method-Local inner class cannot access local variables because the instance of the method-local inner class may still alive after the method is over. But local variables will vanish once the local method is over. I learned that method-local inner class can access final local variable, does this mean final local variable still alive after the method is over? Alexis King Sort of. Java anonymous inner classes act like "closures," that is, they "close" around the current local state. However, Java only lets these classes close around final variables. If it didn't, the local state of the variable

`defined?` and `unless` not working as expected

懵懂的女人 提交于 2019-12-01 15:36:35
I was expecting the following snippet: var = "Not Empty" unless defined? var var # => nil to return "Not Empty" , but I got nil . Any insight into why this is happening? This is one of the only moments in Ruby I would call actual WTFs. You have to use unless defined? var var = :value end With the postfix syntax, the interpreter will internally nil -ify the value so it can reason about the variable, thus making it defined before the check is done: # Doesn't print anything unless defined?(foo) and (p(foo) or true) foo = :value end # Prints nil bar = :value unless defined?(bar) and (p(bar) or