这段代码如何在不使用sizeof()的情况下确定数组大小? [英] How does this piece of code determine array size without using sizeof( )?

查看:107
本文介绍了这段代码如何在不使用sizeof()的情况下确定数组大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过一些C面试问题,我找到了一个问题,指出如何在不使用sizeof运算符的情况下在C中查找数组的大小?",并提供以下解决方案.可以,但是我不明白为什么.

Going through some C interview questions, I've found a question stating "How to find the size of an array in C without using the sizeof operator?", with the following solution. It works, but I cannot understand why.

#include <stdio.h>

int main() {
    int a[] = {100, 200, 300, 400, 500};
    int size = 0;

    size = *(&a + 1) - a;
    printf("%d\n", size);

    return 0;
}

如预期的那样,它返回5.

As expected, it returns 5.

人们指出答案,但语法确实有所不同,即索引方法

edit: people pointed out this answer, but the syntax does differ a bit, i.e. the indexing method

size = (&arr)[1] - arr;

因此,我认为这两个问题都是有效的,并且对该问题的处理方式略有不同.谢谢大家的大力帮助和详尽的解释!

so I believe both questions are valid and have a slightly different approach to the problem. Thank you all for the immense help and thorough explanation!

推荐答案

在指针上加1时,结果是下一个对象在指向类型的对象序列(即数组)中的位置).如果p指向int对象,则p + 1将指向序列中的下一个int.如果p指向int的5元素数组(在这种情况下,表达式为&a),则p + 1将指向下一个int 5元素数组按顺序.

When you add 1 to a pointer, the result is the location of the next object in a sequence of objects of the pointed-to type (i.e., an array). If p points to an int object, then p + 1 will point to the next int in a sequence. If p points to a 5-element array of int (in this case, the expression &a), then p + 1 will point to the next 5-element array of int in a sequence.

减去两个指针(假设它们都指向同一个数组对象,或者一个指针指向数组的最后一个元素),则得出这两个指针之间的对象(数组元素)的数量.

Subtracting two pointers (provided they both point into the same array object, or one is pointing one past the last element of the array) yields the number of objects (array elements) between those two pointers.

表达式&a产生a的地址,并具有类型int (*)[5](指向int的5元素数组的指针).表达式&a + 1产生a之后的下一个5元素数组int的地址,并且类型也为int (*)[5].表达式*(&a + 1)取消引用&a + 1的结果,以便产生a的最后一个元素之后的第一个int的地址,并具有类型int [5],在这种情况下,该类型衰变"为一个int *类型的表达式.

The expression &a yields the address of a, and has the type int (*)[5] (pointer to 5-element array of int). The expression &a + 1 yields the address of the next 5-element array of int following a, and also has the type int (*)[5]. The expression *(&a + 1) dereferences the result of &a + 1, such that it yields the address of the first int following the last element of a, and has type int [5], which in this context "decays" to an expression of type int *.

类似地,表达式a衰减"到指向数组第一个元素的指针,并具有类型int *.

Similarly, the expression a "decays" to a pointer to the first element of the array and has type int *.

图片可能会帮助您

int [5]  int (*)[5]     int      int *

+---+                   +---+
|   | <- &a             |   | <- a
| - |                   +---+
|   |                   |   | <- a + 1
| - |                   +---+
|   |                   |   |
| - |                   +---+
|   |                   |   |
| - |                   +---+
|   |                   |   |
+---+                   +---+
|   | <- &a + 1         |   | <- *(&a + 1)
| - |                   +---+
|   |                   |   |
| - |                   +---+
|   |                   |   |
| - |                   +---+
|   |                   |   |
| - |                   +---+
|   |                   |   |
+---+                   +---+

这是同一存储的两个视图-在左侧,我们将其视为int的5个元素数组的序列,而在右侧,我们将其视为.我还展示了各种表达式及其类型.

This is two views of the same storage - on the left, we're viewing it as a sequence of 5-element arrays of int, while on the right, we're viewing it as a sequence of int. I also show the various expressions and their types.

请注意,表达式*(&a + 1)导致未定义的行为:

...
如果结果指向数组对象的最后一个元素之后,它将 不得用作被评估的一元*运算符的操作数.
...
If the result points one past the last element of the array object, it shall not be used as the operand of a unary * operator that is evaluated.

C 2011在线草案,6.5.6/9

C 2011 Online Draft, 6.5.6/9

这篇关于这段代码如何在不使用sizeof()的情况下确定数组大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆