为什么p和* p打印相同的值 [英] why p and *p are printing same values

查看:129
本文介绍了为什么p和* p打印相同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<stdio.h>
int main()
{
int arr[5]={9,8,7,6,5};
int (*p)[5]=arr;
int *ap[5]={arr+4,arr+3,arr+2,arr+1,arr};
printf("%u\n%u\n%u\n%u\n%u\n%u\n%u\n%d\n",arr,*++ap[3],p,*p,**p,p+1,*p+1,**p+3);
}





[edit]已添加代码块 - OriginalGriff [/ edit]



[edit]Code block added - OriginalGriff[/edit]

推荐答案

C standard:
C standard:

...将类型为'array of type'的表达式转换为带有type的表达式指向类型的指针指向数组对象的初始元素而不是左值。

...an expression that has type 'array of type' is converted to an expression with type 'pointer to type' that points to the initial element of the array object and is not an lvalue.

这意味着数组的名称,当作为值时,将转换为指向第一个的指针(索引0)数组的元素...所以p和* p在这里是相同的。

That means that the name of the array, when stands as a value is converted to a pointer to the first (index 0) element of the array...So p and *p will be the same here.


问题是你的代码是错误的。如果在编译器中启用警告,您应该看到类似于以下消息:

The problem is that your code is wrong. If you enable warnings in your compiler you should see a message similar to:
main.c: In function 'main':                           
main.c:6:13: warning: initialization from incompatible
 pointer type [enabled by default]                    
 int (*p)[5]=arr;                                     



它应该是


It should be

int *p =arr;


这篇关于为什么p和* p打印相同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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