Monday, August 7, 2017

static member for the structure is wrong

static member for the structure is wrong
Note that a static struct itself is different from a static member of a struct. While you can declare a static struct variable:
static struct MyStruct s;
you can't define a struct type with a static member:
struct MyStruct {
    static int i; // <- compiler error
};
The reason for this is that in C, a struct is a type - declaring a type of which a member variable is always the same instance (i. e. static) for multiple instances of that type is simply nonsense. In C++, structs are in reality classes (they only differ in the default visibility scope of members), and in C++ the static keyword means something else in this case. It means a class method - but since C doesn't have classes and methods, this doesn't make sense and is invalid in C.

Ref:https://stackoverflow.com/questions/12138742/the-member-variable-of-a-static-struct-in-c

No comments:

Post a Comment