简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM