C ++解引用数组

编程入门 行业动态 更新时间:2024-10-24 09:18:22
本文介绍了C ++解引用数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我从来没有读过关于解引用数组的指针一样东西,我认为这不应该工作。但接下来的code不使用Qt创建者和g ++ 4.8的工作:

I never read anything about dereferencing arrays like pointers and I believe it shouldn't work. But the following code does work using QT Creator and g++ 4.8:

int ar[9]{1,2,3,4,5,6,7,8,9}; cout << *ar << endl; //prints the first element of ar

它是正确的行为或只是编译器固定code?

Is it proper behavior or just the compiler fixing the code?

推荐答案

您不能解引用一个数组,只有一个指针。

You cannot dereference an array, only a pointer.

这里发生的事情是,数组类型的前pression,在大多数情况下,隐式转换(衰变)一个指向数组对象的第一个元素。因此, AR 衰变来&放大器; AR [0] ;提领,让你的AR值[0] ,这是一个 INT 。

What's happening here is that an expression of array type, in most contexts, is implicitly converted to ("decays" to) a pointer to the first element of the array object. So ar "decays" to &ar[0]; dereferencing that gives you the value of ar[0], which is an int.

这最近我的答案讨论这在一些细节的C.用于C ++类似的规则,但C ++有一些更多的情况下,转换不会发生(其中没有发生在你的code)。

This recent answer of mine discusses this in some detail for C. The rules for C++ are similar, but C++ has a few more cases where the conversion does not occur (none of which happen in your code).

更多推荐

C ++解引用数组

本文发布于:2023-11-30 20:16:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1651353.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!