qiubit
qiubit

Reputation: 4816

C - initializer element is not constant

Here's a snippet of my code in C:

const char *d = "dictionary.dict";

struct dictionary *dict =
        dictionary_load_lang(d); // Compile error here

The type of dictionary_load_lang() is struct dictionary *dictionary_load_lang(const char *lang).

When trying to compile the compiler says "initializer element is not constant" and I can't see why. What's going on?

Upvotes: 0

Views: 356

Answers (1)

Eugene Sh.
Eugene Sh.

Reputation: 18311

dictionary_load_lang() is a function, hence a non-constant. You can't use a non-constants for static storage variables (read: global and/or static):

As per C99 Standard: Section 6.7.8:

All the expressions in an initializer for an object that has static storage duration shall be constant expressions or string literals.

However, you can do such an initialization if within a function and for a non-static variable.

Upvotes: 6

Related Questions