数组名是C ++中的常量指针吗? [英] Is array name a constant pointer in C++?

查看:160
本文介绍了数组名是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 ++中的常量指针吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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