Home > Enterprise >  How to define extern variable within function
How to define extern variable within function

Time:04-15

In my C program, I have an integer array of length 120. Initializing the array is complex enough that it needs its own function, however once it is initialized it never needs to be changed again. I want to initialize the array once upon starting the program, and from that point on I want it to be read-only. There are only two functions that access this array: One initializes it upon bootup (I will call this init()). The other reads it (I will call it eval()). init and eval are in the same file. How can I do what I am trying to do?

-I think I need the variable to be "extern", because it is accessed by two functions. However, extern variables cannot be initialized inside of a function like I need, and it is too complex to initialize in one line when I declare it. I can't initialize it inside of eval because eval runs half a million times per second and it will slow it down significantly.

-I think I need the variable to be "const", because the value isn't changed after initialization.

-So I declare the array as "const extern int xyz[120]" in my header.h file. Then I run init() early on in my main function. Compiler says "undefined reference to xyz", probably because I am trying to initialize an extern variable inside of a function.

-I don't understand the difference between the static keyword and the extern keyword. Should I be using static instead?

CodePudding user response:

Because you're using the extern keyword, what you have in your header file is a declaration, not a definition. It states that a given variable exists but it doesn't actually define it. Without a definition, you get the "undefined reference" error during the linking phase.

Also, if you're "initializing" the array inside of a function, that's not actually initialization but assignment. Initialization only occurs at the point a variable is defined. And because the elements of the array are being assigned to, even if they're never changed after that, you can't declare it const.

If the only functions that access the array reside in the same source file, then there's no need to declare it in a header file. Just define it (i.e. without extern) in the source file. You'll also need to leave off const since the initialization functions needs to assign to it. Using static would also be a good idea in this case so that the array's name isn't visible in any other source files.

As a matter of encapsulation, you might want to put the array and those two functions in a source file by themselves. That way you know that only those two functions can directly access the array.

CodePudding user response:

Answer A: It’s a global

The extern keyword is used to tell the compiler that some global variable exists elsewhere in the code (usually a separate translation unit, which you can think of as a .c/.h → object file that gets linked into the final executable/binary).

Hence, the variable is a global and must simply be initialized before use:

// these exist elsewhere:
extern some_type global_variable;
void initialize_global_variable();

// this exists here:
int main()
{
  initialize_global_variable();
  ...
  do_something_with( global_variable );

I can see how this is unsatisfying because the initialization of the global is not very automatic or encapsulated. But it is the way C is designed, and works fine.

Answer B: Restrict access via function call

The other option is to encapsulate the variable as a static local in a function, and initialize it on first use.

some_type get_variable()
{
  static bool is_initialized = false;
  static some_type variable;
  if (!is_initialized)
  {
    ...
    is_initialized = true;
  }
  return variable;
}

Thereafter you can call the function every time you want the value:

some_type get_variable();

int main()
{
  do_something_with( get_variable() );

The only downside to this one is the check for initialization every time the value is used.

  •  Tags:  
  • c
  • Related