C语言 数组的外部声明?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3071890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:42:01  来源:igfitidea点击:

External Delaration for An Array?

c

提问by Ravi Gupta

I have an array defined in a file and in another I have to use it, for e.g-

我在文件中定义了一个数组,而在另一个文件中我必须使用它,例如-

/* a.c - defines an array */

int a[] = {1,2,3,4,5,6,7,8,9}; 


/* b.c - declare and use it. */

#define COUNT ((sizeof a)/(sizeof int))
extern int a[];  //size of array

.
.
.

int i;
for(i=0; i<COUNT; i++)
  printf("%d", a[i]);
.
.
.

Now when I try to compile it it gave me error saying that sizeof cann't be used on incomplete type.

现在,当我尝试编译它时,它给了我错误,说 sizeof 不能用于不完整的类型。

Can anybody tell me how to handle such case in C/C++? I don't want to array subscript in a.c

谁能告诉我如何在 C/C++ 中处理这种情况?我不想在 ac 中排列下标

Thanks in advance

提前致谢

采纳答案by Mark Wilkins

You might put something like the following into a.cand then extern it from b.c.

您可以将类似于以下内容的内容放入a.c,然后从b.c.

In a.c:

在交流中:

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

And then in b.c:

然后在公元前:

extern int a[];
// the extern (thanks Tim Post) declaration means the actual storage is in another 
// module and fixed up at link time.  The const (thanks Jens Gustedt) prevents it
// from being modified at runtime (and thus rendering it incorrect).
extern const int lengthofa;

void somefunc() {
  int i;
  for ( i = 0; i < lengthofa; i++ )
    printf( "%d\n", a[i] );
}

回答by AnT

If you want your array size to be accessible as a compile-time constant, then you have no other choice but to specify array size explicitly in the externdeclaration of the array

如果您希望您的数组大小可以作为编译时常量访问,那么您别无选择,只能在数组extern声明中明确指定数组大小

extern int a[9];

In this case it becomes your responsibility to make sure that array size is consistent between the externdeclaration and definition. You can use a manifest constant for that, but still it is going to be your responsibility to make sure that the number of initializers between the {}and the declared size are the same.

在这种情况下,您有责任确保extern声明和定义之间的数组大小一致。您可以为此使用清单常量,但您仍然有责任确保{}和声明大小之间的初始化程序数量相同。

If you don't care to have the array size as a compile-time constant, then you can do what Mark Wilkins suggests in his answer.

如果您不关心将数组大小作为编译时常量,那么您可以按照 Mark Wilkins 在他的回答中的建议进行操作。

回答by Engineer

I would #define ARRAY_MAX (or whatever)in an external header generally used for defining such things, then include that header in both files that need it. This works well when you tend to keep most if not all of your defines in one or two central headers.

我会#define ARRAY_MAX (or whatever)在通常用于定义此类内容的外部标头中,然后将该标头包含在需要它的两个文件中。当您倾向于将大部分defines保留在一个或两个中央标题中时,这很有效。

回答by Hairi

Put the variable in the header file only like this:

将变量放在头文件中,如下所示:

#ifndef A_DEF
#define A_DEF
int a[] = {1,2,3,4,5,6,7,8,9}; 
#endif

The conditional compilation will not allow variable redefinition. Thus you have access to it from anywhere.

条件编译不允许变量重新定义。因此,您可以从任何地方访问它。

回答by Amardeep AC9MF

The compiler does not have enough information when compiling b.c to determine the size of the array. That information is only in a.c where your initializer list is in scope.

编译器在编译 bc 时没有足够的信息来确定数组的大小。该信息仅在 ac 中,其中您的初始值设定项列表在范围内。

You will have to communicate the size somehow. One method is to define an int const with the size and extern that as well. Another possibility would be to use a sentinel (a value outside your valid data range) to indicate the end of the array.

您必须以某种方式传达大小。一种方法是定义一个 int const ,其大小和 extern 也是如此。另一种可能性是使用哨兵(有效数据范围之外的值)来指示数组的结尾。

回答by noisy

in my opinion if you don't have definition and define with sizeof a in one file it doesn't compile.

在我看来,如果您没有定义并在一个文件中使用 sizeof a 进行定义,则它不会编译。

Files are compile and stored as *.obj / *.a files. You can use arrays from another files thanks to extern declaration which will be check in linking process, after compilation.

文件被编译并存储为 *.obj / *.a 文件。由于 extern 声明,您可以使用其他文件中的数组,编译后将在链接过程中进行检查。

You want to declare define (preprocesor must help here with this. It is run before compilator).

您想声明定义(预处理器必须在这里提供帮助。它在编译器之前运行)。

so before compilation you won't get array from another file...

所以在编译之前你不会从另一个文件中获取数组......