2

Why does this not work

extern int externed_variable;
int variable = externed_variable;

While this does

extern int externed_variable;
int *variable_ptr = &externed_variable;

The compiler error I get from the former is "expression must have a constant value". I am using MSVC.

3
  • What scope is this? Commented Jun 20, 2017 at 16:16
  • the scope is global Commented Jun 20, 2017 at 16:17
  • 2
    The extern keyword is telling the compiler something like “Don’t’ worry about not finding this symbol here, it is declared elsewhere and we’ll let the linker figure it out”. Then when you use it on the next line, the compiler is saying “I don’t know what this is yet…” Commented Jun 20, 2017 at 16:35

2 Answers 2

3

"expression must have a constant value"

As the error says, When you initialize a variable, it should be constant.

In this case,

extern int externed_variable;
int variable = externed_variable;

You are initializing with a "variable" and variable gets value run-time.

But when you do this:

extern int externed_variable;
int *variable_ptr = &externed_variable;

You are assigning address, addresses for global variable are decided at Compile time and hence, Constant. And you are allowed to init a variable with constant value.

So to answer you, As Addresses for global variables are assigned during compilation and are constant, you won't get error!

Sign up to request clarification or add additional context in comments.

3 Comments

please see my comment to PSkocik
@hacksoi When you declare a global variable and assign it a value, variable is stored in Code segment and initial value in Data segment. When you run the program, variable is loaded in the RAM and then value is fetched from DS. Hence, You won't have value at linking time.
@Swanand Theoretically you could have the value at linking time, but only if you're linking statically or the value isn't exported.
2

Global initializations (more precisely, initializations of variables with static lifetime) require integer constants because they're done at load time.

All globals are generally glued together into a segment and initialized all at once from a binary image. This can only be done if you supply C with an integer constant, not a variable reference.

The rule of the standard that requires this is 6.7.9p4:

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

2 Comments

Wouldn't the value of extern'd variables be known at link time? Why can't the linker resolve the value of it?
@hacksoi Standard linkers deal with labels and addresses only, AFAIK. They wouldn't know how much data to copy. I guess it would be doable to modify your linker and the object file format to allow you to do it, but then you could just solve the problem with a macro.

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.