1

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

struct

  • member 1 (every instance of the struct has its own value of this)

  • static member (every struct shares this member)

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?

user2041427
  • 81
  • 2
  • 11

1 Answers1

3

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 {
    ...
};
dbush
  • 205,898
  • 23
  • 218
  • 273
  • 1
    If this is in a header file then you will need to use `extern int my_struct_common_val;` with definition in exactly one `.c` file – M.M Nov 05 '15 at 21:47