Why can't we declare a static variable inside a structure in the C programming language?

Why can't we declare a static variable inside a structure in the C programming language?

+7
c
source share
3 answers

In C ++, a structure is basically a class with all members public, so a static variable makes sense.

In C, a structure is a continuous piece of memory with fields. A static variable cannot be created without changing it (in order to implement statics, you need to refer to one memory cell from all structures of this type), and this will be a big difference in complexity without any particular benefit.

+11
source share

Because C is not C ++.

Since the standard C does not allow this.

Since it does not have a meaningful interpretation in C.

+4
source share

Because in c, we cannot access a static variable called stucture. In C ++, we can access a static member variable with the class name, as shown below.

ClassName :: staticVariableName

"C" does not provide such an object.

0
source share

All Articles