加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 是== * a ??关于指针的查询

发布时间:2020-12-16 10:09:59 所属栏目:百科 来源:网络整理
导读:int main(){int a[4][3] = {10,20,30,40,50,60,70,80,90,100,110,120};printf("%d",((a==*a) (*a==a[0])));return 0;} 在控制台上打印1. 有人有逻辑解释吗? 解决方法 当在表达式中使用时,数组被转换为指针,除非它们是sizeof和unary的操作数.操作符. a和* a
int main()
{
int a[4][3] = {10,20,30,40,50,60,70,80,90,100,110,120};
printf("%d",((a==*a) && (*a==a[0])));
return 0;
}

在控制台上打印1.
有人有逻辑解释吗?

解决方法

当在表达式中使用时,数组被转换为指针,除非它们是sizeof和unary&的操作数.操作符. a和* a是不同类型的(在衰减之后)但具有相同的地址值.

  • a decays to pointer to first element (first row) of array and is of type int (*)[3].
  • *a dereference the row pointed by a and further decayed to pointer to first element of first row. It is of type int *.
  • a[0] is representing the first row which is of type int [3]. In expression it decays to pointer to first element of first row and is of type int * after decay.

作为数组的地址,第一个字节的地址,因此数组的地址,第一行的地址和第一个元素的地址都具有相同的值.因此,在衰减之后,所有a,* a和a [0]都指向同一位置.

以下是上述说明的图形视图:

Stack Overflow http://cdn.sstatic.net/stackoverflow/img/favicon.ico What exactly is the array name in c?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读