数组衰减到指针的异常? [英] Exceptions to array decaying into a pointer?
问题描述
我在很多帖子中都看到在大多数情况下,数组名称会退化为指针".
我能知道在什么情况下/表达式中数组名称不会退化为指向其第一个元素的指针吗?
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:
-
当它是
&
(地址)运算符的参数时.
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.
这篇关于数组衰减到指针的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!