3

I want to use a static array inside the following structure:

struct N{
    int id;
    static long history[100];
};
struct N Node[R][C];  // R is # of rows, C is # of coloumns

But I got this error:

P.c:38:2: error: expected specifier-qualifier-list before ‘static’
                 static long history[100];

I don't know why? Does it mean I cannot use static inside structures?

5
  • What do you think this keyword does? This is C, not C++, remember. Commented Nov 17, 2015 at 15:39
  • 3
    "Does it mean I cannot use static inside structures ?". Yes it means exactly that Commented Nov 17, 2015 at 15:40
  • I want to use static because I want to keep the value of the array between function invocations Commented Nov 17, 2015 at 15:43
  • My apology. I am new in C .. Commented Nov 17, 2015 at 15:44
  • Why not declare, static struct N Node[R][C]; then? Commented Nov 17, 2015 at 16:31

2 Answers 2

2

Unlike C++ where structs (which are fully equivalent to classes in terms of their functionality) are allowed to have static members, C structs are not allowed to do so.

C allows you to have file-scoped and function-scoped static variables. If history needs to be static, make it static in a function where it is accessed, or in a file if it's accessed by more than one function.

However, if you do need history to be static, your struct becomes functionally equivalent to a single int, because static means "one array shared among all instances of my struct". Good chances are that you needed the array to be non-static, i.e. "each struct has its own history":

struct N{
    int id;
    long history[100];
};
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your explanation.
1

In C, a struct member cannot be static; you're confusing this with the C++ static for classes (and consequently C++ structs), where it means that a property/method is not intended to be unique to a particular instance of the class (object). Note that in C++ it also means everything it meant in C (and more) - see here for details: The static keyword and its various uses in C++

In C, the static keyword merely specifies the storage class of a symbol - if done locally (in a function), it means roughly that the variable is a global but only visible to that function, and if applied to an actual global variable it restricts its scope to the file where it's declared. For more info, see here: What does "static" mean?

If you "want to keep the value of the array between function invocations", then simply don't write to it in these functions; I suspect your problem is trying to design your program in an object-oriented manner, even though you're using C.

1 Comment

In fact you are right, I want to "simulate" OO manner as C structure since I understand Java well. Thank you for your reply.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.