I read a lot of topics about C and decay of the arrays, but I don't understand a big thing yet. "Array decays into pointers" only when they are passed ad arguments into another functions or always when declared (except these cases)? So this mean, if I do the same stuff of a function which accepts an array, but in the main
, in this one the array won't decay into a pointer?
Asked
Active
Viewed 155 times
-4

Arin CelMare
- 96
- 1
- 12

ncasteln
- 109
- 1
- 5
-
*"if I do the same stuff of a function"* Providing example code might clarify your case. – Jarod42 Feb 23 '23 at 15:24
-
In `main` as well. The other question I think was answered in the link you provided. – Emanuel P Feb 23 '23 at 15:25
-
1Anytime you use the array as a pointer it will decay to a pointer (to its first element). This actually includes *indexing* of the array, since for any array (or pointer) `a` and index `i`, the expression `a[i]` is *exactly* equal to `*(a + i)`. – Some programmer dude Feb 23 '23 at 15:25
-
1`if I do the same stuff of a function which accepts an array` I doubt you can do that in C without wrapping in `struct`, but then becomes a moo point. – Quimby Feb 23 '23 at 15:25
-
2There are no functions in C that accept arrays. – Eugene Sh. Feb 23 '23 at 15:26
-
1No, it happens **almost all the time** even in `main`. It happens when using `[]` for instance. Just about the only beginner situation where it does not happen is when using `sizeof`. – john Feb 23 '23 at 15:28
-
2In C++, for example, in code like `char a[] = "abcdefg"; auto b = a;`, then `a` will 'decay' to a pointer in the second statement. – Adrian Mole Feb 23 '23 at 15:28
-
Despite what you might think the `[]` operator is not defined for arrays, it is only defined for pointers. Therefore when you use `[]` on an array the array must decay into a pointer. – john Feb 23 '23 at 15:34
-
The question is vague at best. – Harith Feb 23 '23 at 15:35
-
Is your question answered here: https://stackoverflow.com/q/2035066/17017616 ? – leosch Feb 23 '23 at 15:42
-
In case "if I do the same stuff of a function which accepts an array, but in the main, in this one the array won't decay into a pointer?" means "do function parameter adjustment contain the same exceptions to array decay as when an array is used with sizeof & etc special cases" then it's not a duplicate. – Lundin Feb 23 '23 at 15:44
1 Answers
5
When an array is used in an expression, it is automatically converted to a pointer to its first element except when:
- it is the operand of
sizeof
, - it is the operand of unary
&
(“address of”, not the binary “AND”), or - it is a string literal used to initialize an array (as in
char foo[] = "abc";
).
When a function declaration includes a declaration of a parameter as an array, that is not an expression. There is no array when the declaration is analyzed, so there is no conversion of an array to a pointer. However, there is an adjustment of the declaration. When a parameter declaration declares an array, it is automatically adjusted to declare a pointer instead.
The rule for converting arrays in expressions is in C 2018 6.3.2.1 2. The rule for adjusting arrays in parameter declarations is in 6.7.6.3 7.

Eric Postpischil
- 195,579
- 13
- 168
- 312
-
Is a string literal in array initialization considered an array by itself? – Eugene Sh. Feb 23 '23 at 15:35
-
-
1
-
-
@tstanisl: `_Alignof` cannot be applied to an expression; its operand must be a parenthesized type name. Its inclusion in 6.3.2.1 2 in C 2011 was a mistake. That was corrected in C 2018. – Eric Postpischil Feb 23 '23 at 17:26
-
Yes. But it is rather a pointless change. Since C23 one could simply do `_Alignof(typeof(expr))`. Allowing expressions as operands of `_Alignof expr` will make it a bit more convenient at minimal implementation cost. – tstanisl Feb 23 '23 at 17:32
-
@tstanisl: This is not the place to discuss whether various changes to the C standard are desirable. – Eric Postpischil Feb 23 '23 at 17:35
-
@Eric Can I ask why you think the duplicate closure was wrong? Especially after [this question](https://stackoverflow.com/q/17752978/10871073) was added to the list. – Adrian Mole Feb 24 '23 at 12:01
-
@AdrianMole: Despite the poor phrasing of this question, the adjustment of parameter declarations is a part of this question (“or always when declared”), and the proposed duplicates spoke primarily to the conversion of arrays, inadequately covering the adjustment of declarations. The one you link to in your comment does not mention declaration adjustment at all. – Eric Postpischil Feb 24 '23 at 12:08