Is it ok to return a reference of a function scope static variable?

Posted by kartik on Stack Overflow See other posts from Stack Overflow or by kartik
Published on 2010-05-26T09:40:10Z Indexed on 2010/05/26 9:41 UTC
Read the original article Hit count: 231

Filed under:
|

I wanted to know if that has any ill effects under any circumsatnce.

For ex:

Ex1:
void* func1()
{
   void* p_ref = NULL;
   //function scope static variable
   static int var1 = 2;
   p_ref = &var1;
   return p_ref;
}
Ex2:

//file scope static variable
static int var2 = 2;

void* func2()
{
   void* p_ref = NULL;
   var2 = 3;
   p_ref = &var2;
   return p_ref;
}

So in the above two cases what is the difference apart from the fact that var1 is function scope and var2 is file scope.

Thanks in advance.

© Stack Overflow or respective owner

Related posts about c

    Related posts about embedded