本文介绍了数组衰减为指针的异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在许多帖子中看到在大多数情况下数组名称会衰减为指针".
我可以知道在哪些情况/表达式中数组名称不会衰减为指向其第一个元素的指针吗?

I have seen in many posts that "in most of the cases array names decay into pointers".
Can I know in what cases/expressions the array name doesn't decay into a pointer to its first elements?

推荐答案

好的.

在 C99 中存在三种基本情况,即:

In C99 there are three fundamental cases, namely:

  1. 当它是 &(address-of)运算符的参数时.

  1. when it's the argument of the & (address-of) operator.

当它是 sizeof 运算符的参数时.

when it's the argument of the sizeof operator.

当它是 char [N + 1] 类型的字符串文字或 wchar_t [N + 1] (N 是用于初始化数组的字符串的长度,如 char str[] = "foo";wchar_t wstr[] = L"foo";.

When it's a string literal of type char [N + 1] or a wide string literal of type wchar_t [N + 1] (N is the length of the string) which is used to initialize an array, as in char str[] = "foo"; or wchar_t wstr[] = L"foo";.

此外,在 C11 中,新引入的 alignof 运算符也不会让其数组参数衰减为指针.

Furthermore, in C11, the newly introduced alignof operator doesn't let its array argument decay into a pointer either.

在 C++ 中,还有一些额外的规则,例如,当它通过引用传递时.

In C++, there are additional rules, for example, when it's passed by reference.

这篇关于数组衰减为指针的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 12:27