1

In C, a function automatically decays to a pointer to function, and an array to a pointer to its first element. However, structs and unions don't decay to pointers to themselves.

My question is: Why decay rules were designed like this? I mainly (but not completely) expect quotes from K&R, or from the committee, so this question is not primarily opinion-based.

nalzok
  • 14,965
  • 21
  • 72
  • 139
  • 3
    C11: Because C99 did. C99: Because C89 did. C89: Because K&R C did. K&R C: Because BCPL only had pointers, not arrays – M.M Apr 05 '16 at 04:30
  • 5
    [Similar question](http://stackoverflow.com/questions/33291624/why-do-arrays-in-c-decay-to-pointers) – M.M Apr 05 '16 at 04:30
  • 1
    Don't forget that structures and unions are first-class types in C (they can be assigned to, passed by value, etc.), while arrays can only be passed by reference (unless embedded in structures or unions). So it's not really possibly to apply the array conventions to structs and unions. – Tom Karzes Apr 05 '16 at 04:35
  • What would it mean to pass a function (rather than a pointer to a function) to another function? Would the code for the passed function be copied so that it could be inspected by the called function? – Jonathan Leffler Apr 05 '16 at 04:45
  • 1
    Related link : http://stackoverflow.com/questions/12674094/array-to-pointer-decay-and-passing-multidimensional-arrays-to-functions – msc Apr 05 '16 at 04:49
  • Your claim is not correct. There are some constructs they don't decay. – too honest for this site Apr 05 '16 at 12:57
  • @TomKarzes: Arrays are **not** passed by reference. You cannot pass an array to a function, but only a pointer to its first element. Which is the reason for the decay. One effect of this you cannot get the `sizeof` such an array (which would be possible if it was passed by reference. – too honest for this site Apr 05 '16 at 12:59
  • @Olaf Passing the address of an array is in fact passing a reference to it. It's not exactly the same as "call by reference", but it is in fact passing a reference (i.e., pointer) to the array (its first element, but the pointer value is the same). When I said "passed by reference", what I really meant was "only references to arrays may be passed", i.e., their addresses. – Tom Karzes Apr 05 '16 at 19:02
  • @TomKarzes: While I'd agree if you explicitly use a pointer to array, this is typically not the same for the common way by relying on the implicit conversion. These do **not** pass the array, but a pointer to the first element. This is an important difference as you can see on the many questions here wondering why `sizeof(array_argument)` does not yield to size of the array, but that of a pointer. So, being very precise on this subject is vital. – too honest for this site Apr 05 '16 at 19:36
  • @Olaf For the record, call by reference does *not* require the size of an array to be passed. Early Fortran compilers (which essentially defined "call by reference") did not. In Fortran 66, dummy array arguments were commonly declared with a dimension of 1 to get around this. In later versions, an asterisk was used to declare an "assumed size array". My point it, the only thing passed for array arguments was their address. Any other information was left up to the user. – Tom Karzes Apr 05 '16 at 19:37
  • @Olaf yes, I should have been more precise in my terminology. The term "passed by reference" was a bit misleading, since C does not directly support call by reference. My point was that they cannot be passed by value. but references to them (their addresses) may be passed. – Tom Karzes Apr 05 '16 at 19:41
  • @TomKarzes: Re. "For the record:" This still is a difference, because the type of an array includes its size in C. In general, I tend to avoid the term "by reference" in C, because there is too much confusion in general about references and pointers in programming. I use "by address/by pointer" instead, which is more clear. I don't mind using it among experst, but this is read mostly by beginners. Just had such a question here which used the same phrase out of confusion. – too honest for this site Apr 05 '16 at 19:49
  • @Olaf Yes, I should have avoided the term "by reference". C emulates that behavior for arrays, including syntax that makes it appear that way, but doesn't directly support it. The notation is convenient, but can be misleading. – Tom Karzes Apr 05 '16 at 19:59
  • A function *decays*? Does this mean it *composts* and *smells bad*? Is this *decay* permanent and irreversible? Does it cause the source code to change? You should be able to see that's a strange, confusing term, now... Why don't we stick to using the terms that the standards use? – autistic Feb 20 '17 at 04:07

0 Answers0