简体   繁体   English

指针与其地址之间的差异

[英]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.

相关问题 指针地址和指针包含的地址之间的差异 - Difference between the address of a pointer and the address that a pointer contains 指针地址和双指针地址有什么区别 - What is the difference between address of pointer and double pointer 打印指针地址和&符地址之间的区别 - Difference between printing pointer address and ampersand address 将指针分配给函数或将其地址作为函数参数传递之间有什么区别吗? - Is there any difference between assigning the pointer to the function or passing its address as function argument? 指向char的指针的地址与指向char的指针的指针之间的区别 - difference between address of pointer to char and pointer to pointer to char 将“指针指向指针”和“指针地址”传递给函数之间的区别 - Difference between passing “pointer to pointer” and “address of pointer” to a function 返回地址和返回指针之间的区别 - Difference between returning address and returning pointer 两个指针的指针地址相差很大 - Large difference in pointer address between two pointers 地址运算符和汇编中的指针之间有区别吗? - Is there a difference between the address operator and a pointer in assembly? float指针和int指针地址有什么区别? - What is the difference between float pointer and int pointer address?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM