0

I have defined the following static const array:

const int arr[197] = { 55, -63, 12, -17, 121 , ... };

The array never changes. It contains some specific numerical pattern eligible for my program. This array is used in three different files:

  • foo1() in file1.c
  • foo2() in file2.c
  • foo3() in file3.c

everytime passing the array to a function as an argument.

Because the definition of this array is quite long and extensive, I want to create separate file just to define and keep that array in there.

How should that be done properly? Should I define that array in let's say arr.h and then put #include <arr.h> inside .c files that use this array?

7
  • 1
    I can see your question is not rather about arrays itself, but global variables and accessibility of them. You can make a singleton class and share it, you can make it static and share it as well. There are plenty ways to do that. Commented May 1, 2019 at 3:05
  • 1
    declare as extern in .h and define in .c Commented May 1, 2019 at 3:06
  • 1
    @IłyaBursov So define in arr.c, declare as extern in arr.h and then #include <arr.h> inside .c files that will use the array? :) Commented May 1, 2019 at 3:08
  • 1
    @weno yes, this is how it works in c Commented May 1, 2019 at 3:08
  • 1
    "everytime passing the array to a function as an argument" Yes, that's the correct thing to do. Note that passing the array as an argument actually only passes a pointer to the first object in the array. So the performance impact is minimal. Commented May 1, 2019 at 3:56

2 Answers 2

1

Define const int arr[197] = ... in arr.c.

Declare extern const int arr[197]; in arr.h

Only include arr.h in arr.c, file1.c, file2.c, file3.c with #include "arr.h", not #include <arr.h> almost like @weno

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

3 Comments

thanks. Do you perhaps see any idea to make this array not global? Defining it three times in foo1(), foo2(), foo3() or defining it in main() does not seem okay because this array definition's code is really long and makes everything unreadable. That's why separate file for the array in the first play. But any way to not make it global?
@weno: “Global” is not a proper term in C. The name (identifier) for the array does not need to be visible at file scope other than in the file in which it is defined; it can be declared only inside functions where it is used. But it must have external linkage, so that the declarations refer to the same object as the definition.
weno This answer does not "Defining it three times in foo1(), foo2(), foo3()", It is defined once, in arr.c.
1

I can't comment on the accepted answer (newb), so here are some ways to define the array once, but not as a global variable:

  1. The ugly: Define the array in arr.h, then in foo1/2/3(), just #include "arr.h" (inside the function body).
// arr.h
const int arr[197] = { 55, -63, 12, -17, 121 , ... };

// file1.c
void foo1()
{
#include "arr.h"
    // Use arr like a local variable
}
  1. The less ugly but still pretty ugly: Define arr in arr.h but only include in main. Then, pass arr as an argument to foo1/2/3
int main()
{
#include "arr.h"
    foo1(arr);
    foo2(arr);
    foo3(arr);
}
  1. The text book:
// arr_init.c
void init_arr(int arr[197])
{
    const int src_arr[197] = {55, -63, 12, -17, 121 , ...};
    memcpy(arr, src_arr, sizeof(src_arr));
}

// arr_init.h
extern void init_arr(int arr[197]);

// main.c
#include "init_arr.h"
int main()
{
    int arr[197];
    init_arr(arr);
    foo1(arr);
    foo2(arr);
    foo3(arr);
}

// file1.c
void foo1(const int arr[197])
{
...
}

...
  1. Best: Use global const as @chux said.

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.