user2041427
user2041427

Reputation: 79

How to create a 'static' variable for a struct in C

I would like to have a struct that functions like this:

struct

I am aware that the static keyword does not do this. My question is, how can I mimic this behavior?

Could I create a member that is pointer to a global variable?

Is there some other better way to do this?

Upvotes: 1

Views: 952

Answers (1)

dbush
dbush

Reputation: 223872

Unlike structs in C++ which can have static data members, C structs don't have such a construct.

Since this is a common value for anyone that may use it, just declare it as a global:

int my_struct_common_val = 42;

struct my_struct {
    ...
};

Upvotes: 3

Related Questions