sizeof extern数组

sizeof 的计算发生在代码编译 的时刻。。

extern 标注的符号 在链接的时刻解析。。。 

所以 sizeof 不知道 这个符号到底占用了多少空间。

原因在于没有声明长度的数组是个不完整类型。
匿名长度的数组声明在有初始化值定义时会有编译器根据初始化列表确定其长度。但是对与extern文件外部的数组,只有链接时才能确定其大小(同一文件的extern我等会测试),但sizeof运算符需要在编译时就确定对象的大小,于是错误就出现了。
解决办法就是声明的时候给它个长度。


sizeof cannot be used with function types, incomplete types, or bit-field lvalues.

When applied to an operand that has structure or union type, the result is the total number of bytes in such an object, including internal and trailing padding.

Incomplete Types

An incomplete type is a type that describes an identifier but lacks information needed to determine the size of the identifier. An "incomplete type" can be:

  • A structure type whose members you have not yet specified.

  • A union type whose members you have not yet specified.

  • An array type whose dimension you have not yet specified.

The void type is an incomplete type that cannot be completed. To complete an incomplete type, specify the missing information. The following examples show how to create and complete the incomplete types.

  • To create an incomplete structure type, declare a structure type without specifying its members. In this example, the ps pointer points to an incomplete structure type called student.

    struct student *ps;
    
  • To complete an incomplete structure type, declare the same structure type later in the same scope with its members specified, as in

    struct student
    {
        int num;
    }                   /* student structure now completed */
    
  • To create an incomplete array type, declare an array type without specifying its repetition count. For example:

    char a[];  /* a has incomplete type */
    
  • To complete an incomplete array type, declare the same name later in the same scope with its repetition count specified, as in

    char a[25]; /* a now has complete type */

阅读更多
个人分类: C
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭