Thursday, July 9, 2009

Can we declare static variable as a member of structure in C Program? If no, why?

Hi,


I think you are getting confused with the way we declare static variables within classes in most object oriented languages.





If you look up info on static variables, you would know that static variables in C are stored on the program stack and not on the function stack. Also, structures are value types and not reference types. Unless and until you create a variable of the structure type, nothing gets allocated in the memory. That is why you can not declare a static member inside a C structure.





In an OOP scenario however, the things are different. Classes are reference types and a copy of the static members of the class is loaded as soon as an object of it is created.





I hope it will solve your doubt. If it doesn't, please come on IM. We can discuss it further.

roses

No comments:

Post a Comment