in C, if I declare a static variable inside a local strucutre, where does the static variable get placed ? Since the struct is in the stack, will the static variable also be in the stack ?
            Asked
            
        
        
            Active
            
        
            Viewed 2,570 times
        
    2
            
            
        - 
                    5Though the Q is tagged c, I feel the pressing need to ask *In what language c or c++?*, `static` structure member has no meaning in c unlike c++. – Alok Save Apr 03 '12 at 07:29
- 
                    Yes. sorry I diddn't mention it though i tagged it under C. – Sharat Chandra Apr 04 '12 at 06:17
- 
                    If your intention was C and C++ both then retag it so and add a note saying so. – Alok Save Apr 04 '12 at 06:57
2 Answers
10
            If I declare a static variable inside a local strucutre
In current C the keyword static is meaningless inside a structure. You should get an error from the compiler.
If by "static" you mean "not allocated using malloc": the member of a structure is always stored in the same place as the rest of the structure. If said member is a pointer, it can point to memory in the same region or not.
 
    
    
        cnicutar
        
- 178,505
- 25
- 365
- 392
- 
                    "static" haiku: Wonderful `static`; Keyword, object lifetime, scope; Much more than `auto`. – Michael Burr Apr 03 '12 at 07:58
- 
                    
3
            
            
        In C++ static variables will be initialized when you first time use their class. In C they are not allowed since C compiler needs to store the whole struct in the same type of memory. About their storage see Where are static variables stored (in C/C++)?
 
     
    