What's a modern term for “array/pointer equivalence”?

后端 未结 11 1152
孤城傲影
孤城傲影 2020-12-14 06:12

Just about everyone reading this is probably familiar with these three key facts about C:

  1. When you mention the name of an array in an expression, it evaluates
相关标签:
11条回答
  • If you are referring to a type conversion, why not use the term 'casting'? It already is commonly understood to imply that the compiler is being coerced into treating two related but not-the-same types as if they were a desired type. Yes, there is an equivalence here but 'casting' is used as a way to indicate that the types aren't literally the same but that the compiler should treat a type as a different type and that the procedure is acceptable to the compiler, like up-casting for byte->long

    2nd choice: "Synonym"

    0 讨论(0)
  • 2020-12-14 06:22

    Thanks, everybody. I'm using this "answer" to summarize the answers people gave and my assessment of them, not as a proper answer I expect anyone to upvote or anything.

    A lot of people put serious thought into this question, and I appreciate that. But at this point the only thing I can conclude is that there is no answer, today at least, to the question I had in mind.

    Quite a few people suggested "array decay", and that's a very nice shot, but it doesn't work for me. It refers mostly to fact 1 and maybe fact 3 as listed in the original question, but not really fact 2. You can't say "Yes, due to array decay, array subscripting can be thought of as syntactic sugar for pointer arithmetic." (Or at least, you can't say that if your intent is to be clear and instructive.) You'd have to say something like "Yes, due to array decay and the way pointer arithmetic is defined so as to be consistent with it, array subscripting can be thought of as syntactic sugar for pointer arithmetic", which is, well, a different kettle of fish entirely.

    Although I didn't say so explicitly, I was looking for a term in common, widespread, accepted use, although there was no requirement that it involve words actually used in the Standard. And given that no one coughed up such a term, given that everyone engaged in a certain amount of speculation and "What about ...?", I conclude that there is no single term in widespread use for the concept behind the trio of related facts. (Other than "equivalence", which is ruled out.)

    A number of answerers and commentators said this (that there is no one answer) in various ways. I'm going to go ahead and accept Lundin's answer on that basis, and because others seem to have liked it the best.

    Me, going forward I might try using "The correspondence between arrays and pointers in C" and see if it catches on. Arrays and pointers are as different as men and women, and they're actually pretty far apart, but they do exchange letters frequently. :-)

    0 讨论(0)
  • 2020-12-14 06:23

    I would go with the term of array decay. This term goes well with what it suggests. The C standard doesn't say about it in this context and yes the first day I heard the term I went for searching it in the standard but I couldn't find it (So it's a bit confusing regarding who coined the term etc). Also alternatively one can write due to "most scenarios array is converted into pointer"... - No this is not a single Noun. But this is not letting any misinterpretation to take place. Standard itself says it the "conversion".

    Most of the time I try to say it the long way and then put the word ("array decaying") in bracket. In fact there are answers where I didn't even mention it and just went with the standard's words of conversion into pointer.

    0 讨论(0)
  • 2020-12-14 06:26

    Standard uses the term converted to. Array and pointer equivalence still holds better over array decay to pointers. Standard mention this term no where but it's just a mutual understanding between programmers that when it comes in the context of arrays and pointers it is thought as array and pointer equivalence. And what pointer and array equivalence means is: pointer arithmetic and array indexing [that] are equivalent in C, pointers and arrays are different..

    0 讨论(0)
  • 2020-12-14 06:27

    I agree that terms like "pointer/array equivalence" or "array decay" (into a pointer) are inaccurate and can be very misleading.

    I'd propose the following:

    Access into an array using the [] operator with an index has an equivalent pointer arithmetic expression. However, there is no equivalence between a pointer and an array: I can happily change a pointer's value (content), maybe to save a separate iteration counter, but I cannot do that with the "decayed" array; it is immutable, and in some respects, exhibits similar behaviour to C++ references.

    Both verbs "to decay" and "to convert" in English invariably convey that the original is changed in the process. Using an array's name or using the unary '&' operator don't change their operand.

    Therefore, an expression like "yields a pointer" or "creates a pointer" would be more accurate That is reflected in the fact that in both cases the resulting pointer is a temporary thing, (usually) sitting in a GP-register of the CPU, and cannot be assigned to.

    At least that is my understanding, looking at disassemblies seems to confirm that.

    0 讨论(0)
  • 2020-12-14 06:29
    1. The "array subscripting" operator [] works just as well for pointers as it does for arrays.

    No, in fact it only works for pointers. Whenever you type [] in an expression, you always get a pointer to the first element. This is guaranteed to happen since arr[i] must be equivalent to *(arr + i). The former is "syntactic sugar" for the latter.

    1. A function parameter that seems to be an array actually declares a pointer.

    This is actually a special case, referred to as "array adjustment", where the compiler implicitly changes the declaration of a function parameter of array type into a pointer to the first element. The rationale is surely to make functions compatible with the "array decay" of expressions, but the C standard keeps the terms separate.

    Both cases, expressions and function parameters, are often referred to informally as "array decay". Though sometimes this is only used for expressions and not for function parameters. I don't think there exists a single, consistent use of the term. "Array decay" is the best one I think, although the C standard does not use that term anywhere.


    (I dislike the term "equivalence", because an array can turn into a pointer, but not the other way around. Indeed there's always countless beginners coming up with confused beliefs such as "arrays and pointers are the same thing". Calling them "equivalent" doesn't exactly help.)

    0 讨论(0)
提交回复
热议问题