相关疑难解决方法(0)

在两个模块之间共享 sizeof(array)

我想共享sizeof(array)两个 .c 模块之间的值。该数组在文件 A 中初始化,因此编译器在编译时知道大小,然后我想在另一个 B 文件中使用该数组的大小。

示例:
在 Ac 文件中:

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

for (int i = 0; i < sizeof(arr); i++); // that works
Run Code Online (Sandbox Code Playgroud)

在 Ah 文件中:

extern int arr[];
Run Code Online (Sandbox Code Playgroud)

在 Bc 文件中:

#include "A.h"
.
.
.

for (int i = 0; i < sizeof(arr); i++); // here compiler doesn't know size of this arr
Run Code Online (Sandbox Code Playgroud)

有没有办法使这项工作?我知道为什么这不起作用,但也许有一个偷偷摸摸的技巧来解决这个问题。

c arrays linker compilation

5
推荐指数
1
解决办法
116
查看次数

标签 统计

arrays ×1

c ×1

compilation ×1

linker ×1