2

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 ?

Sharat Chandra
  • 4,434
  • 7
  • 49
  • 66

2 Answers2

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
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++)?

Community
  • 1
  • 1
MrTJ
  • 13,064
  • 4
  • 41
  • 63