通过指针算术计算数组长度 [英] Calculate array length via pointer arithmetic

查看:168
本文介绍了通过指针算术计算数组长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道*(&array + 1)的实际工作方式.我认为这是一种计算数组长度的简单方法,并希望在使用它之前正确理解它.我对指针算术不是很有经验,但是根据我的理解,&array给出了数组第一个元素的地址. (&array + 1)就地址而言将转到数组的末尾.但是,不应*(&array + 1)给出该地址处的值.而是打印出地址.非常感谢您的帮助,使我的脑海中的指针变得清晰起来.

I was wondering how *(&array + 1) actually works. I saw this as an easy way to calculate the array length and want to understand it properly before using it. I'm not very experienced with pointer arithmetic, but with my understanding &array gives the address of the first element of the array. (&array + 1) would go to end of the array in terms of address. But shouldn't *(&array + 1) give the value, which is at this address. Instead it prints out the address. I would really appreciate your help to get the pointer stuff clear in my head.

这是我正在研究的简单示例:

Here is the simple example I'm working on:

int numbers[] = {5,8,9,3,4,6,1};
int length = *(&numbers + 1) - numbers;

推荐答案

(此答案适用于C ++.)

(This answer is for C++.)

  1. &numbers是指向数组本身的指针.它的类型为int (*)[7].
  2. &numbers + 1是指向该数组之后的字节的指针,该数组将位于另一个7个int数组.它仍然具有类型int (*)[7].
  3. *(&numbers + 1)取消引用此指针,产生一个int[7]类型的左值,指向数组后的字节.
  4. *(&numbers + 1) - numbers:使用-运算符会强制两个操作数都进行数组到指针的转换,因此可以减去指针. *(&numbers + 1)转换为指向数组后字节的int*. numbers转换为指向数组第一个字节的int*.它们的区别是两个指针之间的int数-这是数组中int的数.
  1. &numbers is a pointer to the array itself. It has type int (*)[7].
  2. &numbers + 1 is a pointer to the byte right after the array, where another array of 7 ints would be located. It still has type int (*)[7].
  3. *(&numbers + 1) dereferences this pointer, yielding an lvalue of type int[7] referring to the byte right after the array.
  4. *(&numbers + 1) - numbers: Using the - operator forces both operands to undergo the array-to-pointer conversion, so pointers can be subtracted. *(&numbers + 1) is converted to an int* pointing at the byte after the array. numbers is converted to an int* pointing at the first byte of the array. Their difference is the number of ints between the two pointers---which is the number of ints in the array.

尽管没有&numbers + 1指向的有效对象,但这就是所谓的过去"指针.如果p是指向T的指针,并指向类型为T的有效对象,那么即使*p可能是单个对象,也可能是对象的末尾,计算p + 1始终有效.数组.在这种情况下,您将获得过去"指针,该指针不指向有效对象,但仍然是有效指针.您可以将此指针用于指针算术,甚至取消引用它以产生一个左值,只要您不尝试通过该左值进行读取或写入即可.请注意,您只能在对象的末尾再过一个字节.尝试任何进一步的操作都会导致不确定的行为.

Although there's no valid object pointed to by &numbers + 1, this is what's called a "past the end" pointer. If p is a pointer to T, pointing to a valid object of type T, then it's always valid to compute p + 1, even though *p may be a single object, or the object at the end of an array. In that case, you get a "past the end" pointer, which does not point to a valid object, but is still a valid pointer. You can use this pointer for pointer arithmetic, and even dereference it to yield an lvalue, as long as you do not try to read or write through that lvalue. Note that you can only go one byte past-the-end of an object; attempting to go any further leads to undefined behaviour.

这篇关于通过指针算术计算数组长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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