4

I have recently run into some trouble while trying to perform the following logic:

static const int size = getSize();

int getSize() {
    return 50;
}

The error I have received is initialiser element is not constant

Having read online I understand that this issue is because the compiler evaluates the static const expression at compilation and therefore cannot know what the value is supposed to be.

My question is how do I get around this?

If I have a library that contains many functions but they all require this logic how are they supposed to use it without having to calculate it each time?

And even if they have to, what if the logic itself can change throughout runtime but I only ever want the first value I receive from the function?

Perhaps I should clarify that the logic in getSize is just an example, it could also contain logic that retrieves the file size from a specific file.

6
  • If you don’t know the value at compile time it’s not a constant, so remove the const keyword. Commented Feb 22, 2019 at 16:13
  • And ”static const int size = getSize();” will never compile if that line is just placed in a file. This is not Python :) you can only call functions from other functions. Commented Feb 22, 2019 at 16:16
  • @Fredrik Is there a way to ensure that it cannot be changed without the const qualifier? Commented Feb 22, 2019 at 16:17
  • 1
    In C++, you can initialize global variables with a function. In C, you cannot. Commented Feb 22, 2019 at 16:18
  • BTW: you write it could also contain logic that retrieves the file size from a specific file.: but then it's no longer a constant.... Commented Feb 22, 2019 at 16:33

2 Answers 2

5

Unlike in C++ you cannot initialize global variables with the result of a function in C, but only with real constants known at compile time.

You need to write:

static const int size = 50;

If the constant must be computed by a function you can do this:

Dont declare static const int size = ... anymore, but write this:

int getSize()
{
  static int initialized;
  static int size;

  if (!initialized)
  {
    size = SomeComplexFunctionOfYours();
    initialized = 1;
  }

  return size;  
}

int main(void)
{
  ...
  int somevar = getSize();
  ...

That way SomeComplexFunctionOfYours() will be called only once upon the first invocation of getSize(). There is a small price to be paid: each time you invoke getSize(), a test needs to be performed.

Or you can initialize it explicitely like this, but then size cannot be const anymore:

static int size;

void InitializeConstants()
{
  size = SomeComplexFunctionOfYours();
}

int main(void)
{
  InitializeConstants();
  ...
  int somevar = size;
  ...
Sign up to request clarification or add additional context in comments.

3 Comments

what should I do if the logic is more complex than that?
If you must, you can even #define size getSize() to avoid having to change all references in your code. It's not very pleasant but perhaps it's better than the alternatives, in the short run.
@NateEldredge refactoring size to getSize() is not a big deal, I'd advise againt "cheating" with a #define
0

The compiler needs to know the value of your constant variable at the compilation time, because its a constant.

Also you can't initialize a variable with a function.

You should do something like this :

#define SIZE 50

static const int size = SIZE;

Comments

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.