Are members of the structure allowed for statics?

#include<stdio.h>
struct str 
{ 
   static int a ;
    int b ;
 } s ;
int main()
{
static int p , k ;
printf("%d %d",sizeof(p),sizeof(s));
getchar();
return 0;
}

The above code gives errors. But if I redefine the first member of the structure to "int" and not to "static int", then it works fine. Why are static members not allowed in the structure and what is its meaning?

+5
source share
5 answers

In C, there is simply no such function. And there is no meaningful conceptual basis for such a function in C.

, ++ - static : , , . a, SomeClass::a. , . ( ++- , , C, C.)

C . SomeStruct::a C. . . str_a, "" struct str struct str.

, ++ , .. ++ . , , ++. . C , , C .

+6

. , , . , :

struct str
{
    int b;
} s;

int str_a;

, int a;, struct str.

( , ++ , C, , , , , .)

0

, , . . C;)

? , ( ) .

0

, C. , ++ , , a, struct str.

- C, ( , , ).

-, - :

int struct_str_static_a;
struct str {
    int b;
} s;

, a, - b.

, :

int struct_str_static_a;
struct str {
    int *pA;
    int b;
} s;
:
s.pA = &struct_str_static_a;

*(s.pA), s.a. struct str pA, a, . , , .

The third option is to get into the next ISO C working group and shift this to a language change. However, it will take a little effort from you over the next ten years or so, maybe it’s not worth the effort :-)

0
source

You have good answers here: http://cboard.cprogramming.com/c-programming/123691-static-variable-structure.html

generally speaking, you have no benefit from declaring it static, but if you still want it, you can switch to C ++ or declare the whole structure as static.

0
source

All Articles