为什么p和* P给予了同一地址p指向数组? [英] Why p and *p giving the same address when p points to an array?

查看:241
本文介绍了为什么p和* P给予了同一地址p指向数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写这个程序 -

I was writing this program -

#include<stdio.h>

void main()
{ 
    int arr[20]; 
    arr[0]=22; 
    arr[1]=23; 
    int (*p)[20]=&arr; 
    printf("address in p :%u:\n",p);
    printf("address in *p:%u:\n",*p);
}

这code的输出是相同的p和* P!所以,据我所知* P持有改编的基地址是什么,但ARR [0]!
因此* p应该有给22的输出!但它显示像宝洁这样相同的内存地址显示。请告诉我这是为什么发生?这是什么背后的原因。结果
codePAD站点链接: HTTP://$c$cpad.org/LK7qXaqt

The Output of this code is same for p and *p ! So far as I know *p is holding the base address of arr which is nothing but arr[0]!!! So *p should have give the output 22 ! But it's showing the same memory address like p is showing. Please tell me why this is happened? What is the reason behind it.
Codepad Site Link : http://codepad.org/LK7qXaqt

推荐答案

P 是一个指向数组 20 整数。阵列的第一个字节的地址被认为是该阵列的地址。解引用它会给整个阵列本身。因此, * P 重新presents数组改编,所以你可以把的* p 作为数组名。结果
作为传递给函数时,数组名转换为指针,它的第一个元素, * P 腐朽为指针指向第一个元素改编。因此

p is a pointer to an array of 20 integers. Address of first byte of array is said to be the address of the array. Dereferencing it will give the entire array itself. Therefore *p represents the array arr, so you can think of *p as an array name.
As array names are converted to pointers to its first element when passed to a function, *p is decayed to pointer to first element of arr. Therefore

printf("address in *p: %p:\n", (void*)*p);  

将打印的第一个元素的地址的数组改编,而

printf("address in p: %p:\n", (void*)p);  

将打印(数组的即第一字节的)整个阵列的地址。由于价值的第一个字节的和的第一个元素的一样,这就是为什么这两个打印相同的地址。

will print the address of the entire array (i.e first byte of the array). Since value of first byte and first element is same, that's why both are printing the same address.

有关详细的解释:到底是什么在C数组名?

这篇关于为什么p和* P给予了同一地址p指向数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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