数组名是C ++中的常量指针吗?

编程入门 行业动态 更新时间:2024-10-19 06:15:26
本文介绍了数组名是C ++中的常量指针吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个关于数组名称的问题a

I have a question about the array name a

int a[10]

在C ++中如何定义数组名称?常量指针?它是这样定义还是只是我们可以看起来像这样?

How is the array name defined in C++? A constant pointer? It is defined like this or just we can look it like this? What operations can be applied on the name?

推荐答案

C ++标准定义了数组及其行为。看看索引。

The C++ standard defines what an array is and its behaviour. Take a look in the index. It's not a pointer, const or otherwise, and it's not anything else, it's an array.

查看区别:

int a[10]; int *const b = a; std::cout << sizeof(a); // prints "40" on my machine. std::cout << sizeof(b); // prints "4" on my machine.

显然a和b不是同一类型,因为它们有不同的大小。

Clearly a and b are not the same type, since they have different sizes.

在大多数上下文中,数组名称衰减为指向其自己的第一个元素的指针。你可以认为这是一个自动转换。结果是一个右值,意味着它是只是一个指针值,不能被分配,类似于当函数名称衰减到函数指针时。

In most contexts, an array name "decays" to a pointer to its own first element. You can think of this as an automatic conversion. The result is an rvalue, meaning that it's "just" a pointer value, and can't be assigned to, similar to when a function name decays to a function pointer. Doesn't mean it's "const" as such, but it's not assignable.

因此,数组is是一个很像函数的指针,is是一个函数指针,或长是int。也就是说,这不是真的,但你可以使用它作为一个在大多数情况下,由于转换。

So an array "is" a pointer much like a function "is" a function pointer, or a long "is" an int. That is to say, it isn't really, but you can use it as one in most contexts thanks to the conversion.

更多推荐

数组名是C ++中的常量指针吗?

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

发布评论

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

>www.elefans.com

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