如何在C99中声明在另一个文件中定义的全局数组?

时间:2021-05-10 21:30:26

Taking into account the VLA (variable length array) I would like to ask your opinions on the following problem: If an array is defined at global scope in one file:

考虑到VLA(可变长度数组),我想问一下您对以下问题的看法:如果在一个文件中在全局范围内定义了一个数组:

int arr[] = {1, 2, 3};

// in the same file it is no problem to obtain the number of elements in arr by
#define arr_num sizeof(arr)/sizeof(arr[0])
// or
enum {arr_num = sizeof(arr)/sizeof(arr[0])};

The problem is that in other files in the same project I would like to create other arrays again at global scope with the same number of elements as there are in arr. But how can one achieve this in C99 if there is no way to 'extern' the enum or #define. Of course one can #define the number of elements of arr by hand in a header file and later use it in the other files but this is very inconvenient since by changing the number of elements in the array arr one has also to change by hand the value of this #define (this is even more inconvenient when arr is an array of structs).

问题是,在同一个项目的其他文件中,我想在全局范围内再次创建其他数组,其元素数量与arr相同。但是,如果没有办法'extern'枚举或#define,怎么能在C99中实现这一点。当然,可以在头文件中手动#define arr元素的数量,然后在其他文件中使用它,但这非常不方便,因为通过更改数组中的元素数量,一个人也必须手动更改这个#define的值(当arr是一个结构数组时,这更加不方便)。

Thanks very much for any help.

非常感谢您的帮助。

2 个解决方案

#1


2  

VLAs don't help with that: they need to be automatic variables, and hence you can't make a global variable a VLA. I agree with valdo that having a global variable containing the array size (or alternatively a function returning it) is the right approach.

VLA没有帮助:它们需要是自动变量,因此您不能将全局变量设为VLA。我同意valdo,拥有包含数组大小的全局变量(或者返回它的函数)是正确的方法。

#2


0  

AFAIK you can't do this according to C99. Because all the translation modules are compiled in an independent way, whereas sizeof should be known at compile time.

AFAIK根据C99不能这样做。因为所有翻译模块都是以独立方式编译的,而sizeof应该在编译时知道。

You may do something like this:

你可以这样做:

int arr[] = {1, 2, 3};
const int g_arrCount = sizeof(arr)/sizeof(arr[0]);

// other translation unit
extern const int g_arrCount;

If you use a whole program optimization option - most likely the g_arrCount variable will be omitted completely, and instead its direct value will be used.

如果使用整个程序优化选项 - 很可能完全省略g_arrCount变量,而是使用其直接值。

#1


2  

VLAs don't help with that: they need to be automatic variables, and hence you can't make a global variable a VLA. I agree with valdo that having a global variable containing the array size (or alternatively a function returning it) is the right approach.

VLA没有帮助:它们需要是自动变量,因此您不能将全局变量设为VLA。我同意valdo,拥有包含数组大小的全局变量(或者返回它的函数)是正确的方法。

#2


0  

AFAIK you can't do this according to C99. Because all the translation modules are compiled in an independent way, whereas sizeof should be known at compile time.

AFAIK根据C99不能这样做。因为所有翻译模块都是以独立方式编译的,而sizeof应该在编译时知道。

You may do something like this:

你可以这样做:

int arr[] = {1, 2, 3};
const int g_arrCount = sizeof(arr)/sizeof(arr[0]);

// other translation unit
extern const int g_arrCount;

If you use a whole program optimization option - most likely the g_arrCount variable will be omitted completely, and instead its direct value will be used.

如果使用整个程序优化选项 - 很可能完全省略g_arrCount变量,而是使用其直接值。