Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usage of "static" within a struct in C

Tags:

c

Is it legal to have a "static" member within a C struct?

For example

struct my_struct {
    int x;
    static int y;
};

If indeed it is legal,then what are the implications of the usage of the "static" keyword?

like image 830
Mno Avatar asked May 16 '11 05:05

Mno


4 Answers

No, that would make no sense in C. It's valid in C++ though.

like image 175
Paul R Avatar answered Oct 02 '22 02:10

Paul R


No, not in C

(You can have a static member in a C++ structure.)

like image 42
Mitch Wheat Avatar answered Oct 02 '22 04:10

Mitch Wheat


You're probably getting confused by the fact that Static isn't used for the same purposes that it is in languages such as Java or C# (or C++ for that matter). This post explains C's usage of static thoroughly:

What does "static" mean?

like image 4
Snukus Avatar answered Oct 02 '22 04:10

Snukus


It seems like you're asking about the intuition behind a static member. A static member means one-per-type instead of one-per-instance. In your case, if you had

struct my_struct a, b;

then a and b would each have their own x but would share a common y. This is also true of static member functions.

But like was stated, this doesn't apply to C. It does to C++ and Java, though.

like image 2
Adam Avatar answered Oct 02 '22 02:10

Adam