数组衰减为指针的异常?

我在许多帖子中看到“在大多数情况下,数组名称会衰减为指针”。< br > 我能知道在什么情况下数组名称不会衰减成指向其第一个元素的指针吗?

38625 次浏览

Sure.

In C99 there are three fundamental cases, namely:

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

  2. when it's the argument of the sizeof operator.

  3. 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";.

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

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