Reputation: 35
When is memory for a structure allocated in C? Is it when I declare it or when I create an instance of it? Also, why can static variables not be used within a struct?
Upvotes: 1
Views: 91
Reputation: 442
When you define a structure you are NOT allocating memory for it, that's the reason why you can use typedef to avoid writing struct my_struct_name. When you define a structure, you're declaring a data type that's why they don't take up data until you declare an instance of that structure.
struct point{ int x; int y; };
This will not take up space until in a function or main you declare one like for example
int main(void){
struct point mypoint1,mypoint2;//THIS IS WHEN MEMORY STARTS BEING ALLOCATED
return 0;
}
Regarding the static, I don't think there's actually a point to declare a static to a structure? Why would you make an variable static to a structure?
Upvotes: 1