[英]Difference between a pointer and its address
I am just trying to unveil the secrets of C and pointers (once again), and I had a question regarding pointers and decaying. 我只是想揭开C和指针的秘密(再一次),我有一个关于指针和腐朽的问题。 Here is some code:
这是一些代码:
#include <stdio.h>
int main() {
int array[] = { 1, 2, 3 };
int (*p_array)[] = &array;
printf("%p == %p == %p\n", array, &array, &array[0]);
printf("%p == %p\n", p_array, &p_array);
return 0;
}
When I run that, I get this output: 当我运行它时,我得到这个输出:
0x7fff5b0e29bc == 0x7fff5b0e29bc == 0x7fff5b0e29bc
0x7fff5b0e29bc == 0x7fff5b0e29bc == 0x7fff5b0e29bc
0x7fff5b0e29bc == 0x7fff5b0e29b0
0x7fff5b0e29bc == 0x7fff5b0e29b0
I understand that array, &array, &array[0]
are all the same, because they decay to a pointer which point to the exact same location. 我知道
array, &array, &array[0]
都是一样的,因为它们会衰减到指向完全相同位置的指针。
But how does that apply to actual pointers, here *p_array
, which is a pointer to an array of int
, right? 但是这怎么适用于实际的指针,这里是
*p_array
,它是指向int
数组的指针,对吗? p_array
should point to the location where the first int
of the array is stored. p_array
应指向存储数组的第一个int
的位置。 But why is p_array
's location unequal to &p_array
? 但是为什么
p_array
的位置不等于&p_array
?
Maybe it is not the best example, but I would appreciate if someone would enlighten me... 也许这不是最好的例子,但如果有人能开导我,我将不胜感激......
Edit: p_array
refers to the address of the first element of the array, whereas &p_array
refers to the address of the p_array
pointer itself. 编辑:
p_array
指的是数组第一个元素的地址,而&p_array
指的是p_array
指针本身的地址。
All the best, David 一切顺利,大卫
I understand that array, &array, &array[0] are all the same, because they decay to a pointer which point to the exact same location.
我知道数组,数组和数组[0]都是一样的,因为它们会衰减到指向完全相同位置的指针。
No. You didn't understand. 不,你不明白。
array
is of array type and in most cases will convert to pointer to its first element. array
是数组类型,在大多数情况下将转换为指向其第一个元素的指针。 Therefore, array
and &array[0]
are same as an expression, except when operand of sizeof
operator. 因此,除了
sizeof
运算符的操作数外, array
和&array[0]
与表达式相同。 &array
is the address of array array
and is of type int (*)[3]
. &array
是数组array
的地址,类型为int (*)[3]
。 Read here in details: What exactly is the array name in c? 在此详细阅读: c中的数组名称究竟是什么? .
。
p_array
is pointer to array array
and store its address while &p_array
is the address of p_array
. p_array
是指针数组array
和同时存储其地址&p_array
是的地址p_array
。
But why is
p_array
's location unequal to&p_array
?但是为什么
p_array
的位置不等于&p_array
?
&p_array
is the address of the pointer itself ( 0x7fff5b0e29b0
), whereas p_array
is a pointer to array
and its value is the address of array
1 , ( 0x7fff5b0e29bc
). &p_array
是指针本身的地址( 0x7fff5b0e29b0
),而p_array
是指向array
的指针,其值是array
1的地址( 0x7fff5b0e29bc
)。
1. The address of the first element. 1.第一个元素的地址。
You simply introduced another variable. 你只是介绍了另一个变量。 As such it has a value ( 0x7fff5b0e29bc ) and an address ( 0x7fff5b0e29b0 ).
因此它具有值(0x7fff5b0e29bc)和地址(0x7fff5b0e29b0)。 This will be so for any variable you introduce
对于您引入的任何变量,这都是如此
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.