不是数组/ arrayName中总是一个指向C中的第一个元素? [英] Isn't an array/arrayname always a pointer to the first element in C?

查看:202
本文介绍了不是数组/ arrayName中总是一个指向C中的第一个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是怎么回事下面是不是总是arrayName中的指针在C的第一个元素?

  INT myArray的[10] = {0};的printf(%d个\\ n,&安培; myArray的); / *打印memadress为第一要素* /
的printf(%d个\\ N,myArray的); / *这个打印memadress也表明,​​名字是一个指针* /的printf(%d个\\ N的sizeof(myarray的)); / *这个打印整个数组,而不是一个指针的大小了吗? * /
的printf(%d个\\ N的sizeof(安培; myArray的)); / *这个打印指针的大小* /


解决方案

数组名是数组名。数组名称是标识整个数组对象的标识符。它不是一个指针到任何东西。

当数组名是一个前pression数组类型就会自动隐式转换为指针到单元类型中的几乎的所有上下文(这通常被称为数组类型使用衰变)。所得到的指针是一个完全独立的临时右值。它无关,与数组本身。它无关,与数组名。

两个例外,当隐式转换不会发生为:运营商的sizeof 和一元运算符&安培; (地址的)。这正是你在code测试。

What's going in below isn't an arrayname always a pointer to the first element in C?

int myArray[10] = {0};

printf("%d\n", &myArray); /* prints memadress for first element */
printf("%d\n", myArray); /* this prints a memadress too, shows that the name is a pointer */

printf("%d\n",sizeof(myArray)); /* this prints size of the whole array, not a pointer anymore? */
printf("%d\n",sizeof(&myArray)); /* this prints the size of the pointer */

解决方案

Array name is array name. Array name is an identifier that identifies the entire array object. It is not a pointer to anything.

When array name is used in an expression the array type gets automatically implicitly converted to pointer-to-element type in almost all contexts (this is often referred to as "array type decay"). The resultant pointer is a completely independent temporary rvalue. It has nothing to do with the array itself. It has nothing to do with the array name.

The two exceptions when the implicit conversion does not take place is: operator sizeof and unary operator & (address-of). This is exactly what you tested in your code.

这篇关于不是数组/ arrayName中总是一个指向C中的第一个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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