有人可以向我澄清这个数组/指针的想法吗?
为了解释数组只是指向我们类的指针(在 C++ 中),我的教授向我们展示了这一点:
array[5] // cout'ing this
*(array + 5) // would return the same value as this
我在完全理解它方面遇到了一些困难。我的想法是这样的:
array
是第一个位置的地址,因此如果我们向该地址添加 5,我们就会在内存中移动 5 个地址。指针运算符从内存位置提取数据。
这是正确的想法吗?这个想法对我来说仍然很模糊,只是觉得我没有完全理解它。我认为听别人解释可能会帮助我更好地理解它。提前致谢!
In attempt to explain that arrays are just pointers (in C++) to our class, my professor showed us this:
array[5] // cout'ing this
*(array + 5) // would return the same value as this
I'm having a little trouble completely understanding it. Here's my thinking:
array
is the address of the first location and so if we add 5 to that address, we move 5 addresses in memory. The pointer operator pulls the data from the memory location.
Is this the correct idea? The idea still feels foggy with me and just feel like I don't understand it completely. I think hearing someone else explain it might help me understand it more. Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你的想法是正确的。
数组隐式转换为指针。有趣的是,
[]
适用于指针,而不是数组。a
[b]
下标运算符定义为*(a + (b))
。[]
用作语法糖 - 编写array[5]
而不是*(array + 5)
指针算术 更令人愉快指针和整数
p + i
会将p
处的地址增加i * sizeof(*p)
字节。*
运算符执行间接寻址。它会给你指针所指向的内容。You've got the right idea.
Arrays implicitly cast to pointers. Interestingly,
[]
works on pointers, not arrays.a
[b]
Subscript operator is defined as*(a + (b))
.[]
is used as syntactic sugar - it's much more pleasant to writearray[5]
instead of*(array + 5)
Pointer arithmetic with a pointer and an integer
p + i
increases the address atp
byi * sizeof(*p)
bytes.The
*
operator performs indirection. It will give you what the pointer is pointing to.你的教授是正确的,这两个表达式会产生相同的结果。你对它的解释让我相信你很好地掌握了其中的机制。
说数组与指针相同并不完全正确。数组很容易转换为指针,这会导致一些混乱。
例如考虑这段代码:
Your professor is correct that the two expressions will produce the same results. Your explanation of it leads me to believe you have a good grasp of the mechanics.
It is not quite correct to say an array is the same as a pointer. An array is very easily converted to a pointer, which leads to some confusion.
For example consider this code:
C++ 中的内存数组是程序的连续内存部分。这就是为什么你可以使用*(array + 5),因为它距离数组前面第5个。
另请记住,C++ 中的数组从 @ 0 开始,因此数组中的第 6 个元素是 array[5]
示例
要访问项目 2,您可以使用
,但请记住 array[index_number] 是在数组中查找项目的标准语法,因此请确保使用 array[index_num] 而不是
*(array + index_num)
from memory arrays in c++ are sections of continuous memory the program. thats why you can go *(array + 5) as it is 5 from the front of the array.
Also keep in mind that arrays in C++ start @ 0 so thus the 6th element in an array is array[5]
example
To access item 2 u would either
but please keep in mind array[index_number] is standard syntax for looking up an item in an array so make sure u use
array[index_num]
instead of*(array + index_num)
是的,你的教授说的是正确的。
详细解释一下
考虑你的数组,
这里它在内存中占用 5 个位置,例如 0x00 0x04 0x08 0x0C 和 0x10 0x14
假设整数占用 4 个字节,这些位置相距 4 个字节。
在这种情况下,“array”代表数组的基地址(指针),即0x00。 a 的类型是 int *。 (因为数组元素的类型是整数)
如果你做数组+1,它将是0x04,因为指针的增量取决于指针的类型。如果指针是整数,它将增加 4 个字节。如果整数类型是字符,则指针将增加 1 个字节。
因此,如果执行 (array+5) ,它将指向地址 0x14 并
返回位置 0x14 的值,即数组第 5 个位置的值。
所以实际上, array[5] 等于 *(array+5)
编译器在内部将 array[5] 转换为 *(array+5)
所以即使我们写 5[array],它也会将其转换为 *( 5+array)
虽然看起来很奇怪,但这就是 5[array] 与 array[5] 作用相同的原因
Yes, what your proffessor said is correct.
to explain it in detail
Consider your array,
here it takes 5 location in the memory say 0x00 0x04 0x08 0x0C and 0x10 0x14
Assuming integer takes 4 bytes, the locations are 4 bytes apart.
it this case 'array' represents the base address (pointer) to the array, that is 0x00. And the type of a would be int *. (since the type of the array elements is integer)
If you do array+1, it will be 0x04, since the increment of the pointer depends on the type of the pointer. If the pointer is integer, it will increment by 4 bytes. If the type of the integer is character, the pointer would increment by one bytes.
so if you do (array+5) it would point to the address 0x14 and
returns the value of the location 0x14 which is the value at 5th location of array.
so in practical, array[5] is equal to *(array+5)
The compiler internally converts array[5] to *(array+5)
so even if we write 5[array], it will get converted it to *(5+array)
Though it seems strange, this is the reason why 5[array] works same as array[5]