The reason you are not getting a warning in the first snippet is because you aren't (from the compiler's perspective) returning an address to a local variable.
You are returning the value of int * temp
. Even though this variable might be (and in this example is) containing a value which is an address of a local variable, the compiler will not go up the code execution stack to see whether this is the case.
Note: Both of the snippets are equally bad, even though your compiler doesn't warn you about the former. Do not use this approach.
You should always be careful when returning addresses to local variables; as a rule, you could say that you never should.
static
variables are a whole different case though, which is being discussed in this thread.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…