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 */

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在 C 语言中,可以使用 sizeof 运算符来求数组的长度。 例如,如果你有一个数组名为 `array` 的数组,你可以使用如下代码求出数组的长度: ``` int length = sizeof(array) / sizeof(array[0]); ``` 其中,`sizeof(array)` 表示整个数组占用的字节数,`sizeof(array[0])` 表示数组中一个元素占用的字节数。那么,将整个数组占用的字节数除以一个元素占用的字节数,就可以得到数组的长度。 你还可以使用如下方式来求出数组的长度: ``` int length = sizeof(array) / sizeof(int); ``` 这种方式同样可以得到数组的长度,但是要注意,这种方式只能在数组的元素类型为 int 类型的情况下使用。 另外,还有一种方法是使用 `extern` 关键字,但是这种方法有一些限制,并不是所有情况都适用。如果你感兴趣,可以了解一下。 ### 回答2: 在C语言中,可以使用sizeof运算符来求解数组的长度。sizeof运算符用于获取变量或数据类型的大小(以字节为单位)。对于数组来说,sizeof可以用来获取整个数组的大小,即数组中所有元素的总大小。 例如,我们有一个整型数组arr,可以通过sizeof(arr)来获取数组arr的长度。假设数组arr包含5个元素,每个元素的大小为4字节,则sizeof(arr)的结果将是20字节(5个元素 x 4字节/元素 = 20字节)。 需要注意的是,sizeof计算的是数组占用的存储空间大小,并不是数组中实际存储的元素个数。换句话说,sizeof(arr)返回的是数组在内存中所占的总字节数,而不是数组中元素的数量。 所以,对于一个已知数组的长度,我们可以使用sizeof运算符来获取数组的长度。但是对于一个未知长度的数组sizeof不能直接用来计算数组的实际元素数量。 如果我们想要在程序中获取数组的实际元素数量,可以通过使用一个变量来记录数组的长度,或者利用数组的结束标志(如在C字符串中使用的'\0')来确定数组的长度。这些方法可以根据具体的数组特点和需求来选择使用。 ### 回答3: 在C语言中,可以使用两种方法来求解数组的长度。 第一种方法是使用sizeof关键字。sizeof关键字可以返回一个变量或数据类型所占用的字节大小。通过将整个数组的字节大小除以单个元素的字节大小,就可求得数组的长度。例如: ```c int arr[] = {1, 2, 3, 4, 5}; int length = sizeof(arr) / sizeof(arr[0]); ``` 这里,sizeof(arr)返回整个数组占用的字节大小,而sizeof(arr[0])返回arr[0]的字节大小。通过相除得到的结果就是数组的长度。 第二种方法是使用循环来遍历数组直到遇到结束标记。在C语言中,以字符型数组为例,通常会以字符串的形式存储。字符串以'\0'(空字符)作为结尾。因此,可以通过遍历数组直到遇到'\0'字符来判断数组的长度。例如: ```c char str[] = "Hello"; int length = 0; while (str[length] != '\0') { length++; } ``` 在循环中,逐个检查数组中的每个字符,当遇到'\0'字符时循环结束,此时length的值就是数组的长度。需要注意的是,这种方法只适用于字符型数组。 无论是使用sizeof关键字还是循环遍历,这两种方法都能够有效地求得数组的长度。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值