找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 477|回复: 4

Question1.24 Why doesn't sizeof work on array in file2.c?

[复制链接]

210

主题

371

回帖

0

积分

管理员

积分
0
发表于 2013-6-30 11:28:21 | 显示全部楼层 |阅读模式

Q: I have an extern array which is defined in one file, and used in another:
file1.c:    int array[] = {1, 2, 3};
file2.c: extern int array[];
Why doesn't sizeof work on array in file2.c?
A: An extern array of unspecified size is an incomplete type; you cannot apply sizeof to it. sizeof operates at compile time, and there is no way for it to learn the size of an array which is defined in another file.

You have three options:
1 、Declare a companion variable, containing the size of the array, defined and initialized (with sizeof) in the same source file where the array is defined:
file1.c:    int array[] = {1, 2, 3}; int arraysz = sizeof(array);   
file2.c:extern int array[];extern int arraysz;
2、#define a manifest constant for the size so that it can be used consistently in the definition and the extern declaration:
file1.h:#define ARRAYSZ 3   extern int array[ARRAYSZ];
file1.c:    #include "file1.h"    int array[ARRAYSZ];   
file2.c:#include "file1.h"
3、Use some sentinel value (typically 0, -1, or NULL) in the array's last element, so that code can determine the end without an explicit size indication:
file1.c:int array[] = {1, 2, 3, -1};
file2.c:extern int array[];

(Obviously, the choice will depend to some extent on whether the array was already being initialized; if it was, option 2 is poor.)
See also question 6.21.

210

主题

371

回帖

0

积分

管理员

积分
0
 楼主| 发表于 2013-7-1 13:02:18 | 显示全部楼层
问题:我先在一个文件中用extern声明了一个数组,然后在另一个文件中定义了该数组,然后在该文件中使用:
       file1.c:int array[]={1,2,3}; file2.c:extern int array[];
为什么在文件file2.c中用sizeof不能计算数组的空间大小?
回答:一个用extern声明的没有详细说明的数组是一个不完整的类型;你不能使用sizeof来计算它的大小。并且sizeof是在编译的时候起作用的,所以没有办法计算在另一个文件中定义的数组。你可以有三个选择来达到你的目的:
1、声明一个共用的变量,用它来存储数组的大小,然后在该数组定义的源文件中定义并且用sizeof来初始化该变量:
      file1.c:int array[]={1,2,3};int arraysz=sizeof(array);
      file2.c:extern int array[]; extern arraysz;
(参考问题6.23)
2、为数组的大小宏定义一个常量,那么它就可以在extern声明的文件和定义的文件中一致地使用了:
      file1.h:#define ARRAYSZ  3    extern int array[ARRAYSZ];
      file1.c:#include "file1.h"  int array[ARRAYSZ];
      file2.c:#include "file1.h"  
3、在数组的最后一个元素使用警示值(这里改成哨兵或者哨兵元素)(像0、-1或者 NULL),因此代码就可以在没有明确指定数组大小的情况下判断是否到数组尾部了:
       file1.c:int array[]={1,2,3,-1};
       file2.c:extern int array[];
(很显然,该选择在一定程度上取决于该数组是否被初始化了,如果是的话,那么第二个选择就是不好的。)
参考问题6.21。

14

主题

65

回帖

0

积分

新手上路

积分
0
发表于 2013-7-2 00:28:09 | 显示全部楼层
三种方法都不错。
第三种方法里面,那个应该翻译成“哨兵”或者“哨兵元素”好些吧?我们一般是这么叫的,呵呵。

210

主题

371

回帖

0

积分

管理员

积分
0
 楼主| 发表于 2013-7-2 09:40:52 | 显示全部楼层

回 boyfaceone 的帖子

boyfaceone:三种方法都不错。
第三种方法里面,那个应该翻译成“哨兵”或者“哨兵元素”好些吧?我们一般是这么叫的,呵呵。 (2013-07-02 00:28)
嗯,这些方法我都没用过呢。我刚看了原版的翻译也是哨兵元素,就改成哨兵吧。。。

0

主题

11

回帖

0

积分

新手上路

积分
0
发表于 2013-8-6 14:45:59 | 显示全部楼层
恩,学习了.
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

果子博客
扫码关注微信公众号

Archiver|手机版|小黑屋|风叶林

GMT+8, 2026-2-2 02:53 , Processed in 0.078521 second(s), 20 queries .

Powered by 风叶林

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表