What is the modern term for "array / pointer equivalence"?

Almost everyone who reads this is probably familiar with these three key facts about C:

  • When you specify the name of an array in an expression, it computes (most of the time) a pointer to the first element of the array.
  • The [] operator of the array substring <works just as well for pointers as it does for arrays.
  • The function parameter, which seems to be an array, actually declares a pointer.

These three facts are absolutely important for handling arrays and pointers to C. They are not even three separate facts; they are interconnected facets of one central concept. It is not possible to do even fairly basic C programming without a proper understanding of this concept.

My question today is simple: what is the name of this concept?

I assumed that I am old-fashioned, but I always called it "equivalence between arrays and pointers in C", or "layout / pointer equivalence" for short. But I found out that you almost cannot say these words in SO; they are largely taboo.

This may seem like an abstract or philosophical question, so for a more specific definition of what I'm looking for, this is a simple noun or noun phrase that I could use in the sentence "Yes, because of _____, array counting can be considered syntactic sugar for pointer arithmetic "in response to, say, this question .

(But please note that I am not looking for answers to this question or answers to the question “What is wrong with the word“ equivalence ”?” Yes, I know, it can mislead students to imagine that arrays and pointers somehow the same. I had this confusion when I wrote this entry on the list of frequently asked questions .)

+39
c arrays pointers terminology
source share
11 answers
  1. The array subscription operator [] works just as well for pointers as it does for arrays.

No, actually this only works for pointers . Whenever you enter [] 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 first is syntactic sugar for the second.

  1. A function parameter that looks like an array actually declares a pointer.

Actually, this is a special case called “array tuning”, when the compiler implicitly changes the declaration of a function parameter of an array type to a pointer to the first element. The rationale, of course, is to make functions compatible with the "array decay" of expressions, but the C standard saves the terms separately.

Both cases, expressions and function parameters, are often informally called "array decay". Although sometimes it is used only for expressions, and not for function parameters. I do not think that there is a single, consistent use of this term. I think "array decay" is the best, although the term C is not used anywhere in the C standard.


(I don't like the term “equivalence” because an array can turn into a pointer, but not vice versa. Indeed, there are always countless newbies who come up with confusing beliefs such as “arrays and pointers are the same thing.” Call their "Equivalent" doesn't quite help.)

+28
source share

There is not a single word for this in the C standard. He uses the word “transformation” when defining behavior (1) in 6.3.2.1p3 “equivalently” in defining behavior (2) in 6.5.2.1p2 and “tuning” in defining behavior (3) in 6.7.6.3p7.

I am old-fashioned, and I don’t think that something is wrong with calling this "equivalence / pointer", if in the context it is clear that you are talking about expressions where (1) a function occurs or is declared, where (3). However, a more acceptable term for people who don't like “equivalence” may be “array to pointer conversion”, as it confuses people more often when it is (1), I think.

+13
source share

I would go with the term of array decay . This term goes well with what it offers. Standard C does not talk about this in this context, and yes, on the very first day I heard this term, I went looking for it in the standard, but I could not find it (so this is a bit confusing who coined the term, etc. d.). Also, as an alternative, you can write down because "most scripts are converted to a pointer" ... - No, this is not one Noun. But this does not allow any misinterpretation. The standard itself says this is a “conversion."

In most cases, I try to say this the long way, and then put the word ("array decay") in brackets. Actually there are answers in which I didn’t even mention it, and just went with standard index conversion words.

+9
source share

I suggest "decay of the matrix to a pointer" is a reasonable abbreviation, since "decay" is the usual jargon in terms of type conversion, with a precedent elsewhere in the C FAQ: Question 6.12 :

Q: Since array references break into pointers, if arr is an array, what's the difference between arr and & arr?

The expanded technical meaning of "decay", which includes this, has been accepted into the nomenclature of the C ++ standard library with C ++ 11: std::decay

+5
source share

There is no single name specified for the three mentioned concepts, but I think that their description, implying any kind of "equivalence", is counterproductive, at least when describing the "modern" C. Although the results of array decay usually behave like pointers, C99 indicates that pointers obtained by decaying an array should not behave the same as pointers reached by other means. Given:

 char fetch_byte_at(void *p, int x) { return ((char*)p)[x]; } struct {char arr[5][7];} s; char test1(int x) { return s.arr[0][x]; } char test2(int x) { return fetch_byte_at(&s, x); } 

The standard does not specify any reason why the value of the pointer s.arr[0] used in test1 should not be equivalent to the value of the pointer ((char*)(void*)s) used when calling fetch_byte_at from test2 . However, it also indicates that test2 should be able to read all bytes of s [meaning that it should work predictably with x values, at least up to 34), saying that s.arr[0][x] only value for x values ​​up to 6.

While nothing in the Standard contradicted either of your first two points individually, it is impossible for both to be held. Either the result of the decay of the array is something other than a "normal" pointer to the first element of the array, or the application of the [] operator to the array has different semantics from applying it to the pointer. Although it is unclear which of Numbers 1 and 2 were canceled by C99, they no longer form any “equivalence” in modern C.

If you recognize the difference between traditional C and "modern" C, it may be helpful to set the conditions for things that make the former useful. However, such things go beyond what you are talking about. Unfortunately, I do not know any standard terms for such things and I will not look for a Standard to provide such terminology when it no longer supports the concepts involved.

+5
source share

Thanks, everyone. I use this "answer" to summarize the answers that people gave, and my assessment of them, and not as the correct answer. I expect someone to be able to support or something else.

Many people have seriously thought about this issue, and I appreciate it. But at this stage I can only conclude that there is no answer, at least today, to the question that I had in mind.

Quite a few people have suggested “array decay”, and this is a very good shot, but it does not work for me. This relates mainly to fact 1 and, possibly, to fact 3, indicated in the original question, but not actual fact 2. You cannot say "Yes, due to the decay of the array, the subtype of the array can be considered as syntactic sugar for pointer arithmetic" . (Or at least you cannot say that if your intention should be clear and instructive.) You would need to say something like "Yes, due to the decay of the array and the way you define pointer arithmetic to match it, an array substring can be considered syntactic sugar for pointer arithmetic, which, of course, is a completely different teapot.

Although I did not say this explicitly, I was looking for a general term, widespread, accepted use, although there was no requirement that it include words actually used in the Standard. And given that no one has coughed such a term, given that everyone is involved in a certain amount of speculation and “What about ...?”, I conclude that there is no single term widely used for the concept underlying the trio of related facts. (Except for “equivalence,” which is excluded.)

Several respondents and commentators said this (that there is not a single answer) in different ways. I am going to go further and accept Lundin's answer on this basis, and because others seem to like it best.

I, in the future, I could try using "Matching between arrays and pointers in C" and see if it will catch. Arrays and pointers are different from men and women, and they are actually quite far apart, but often exchange letters. :-)

+2
source share

This is a subtle question. When I looked through the English thesaurus and the dictionary, I came to the conclusion that the term similarity may be appropriate for your context.

By the definition of the Oxford Online Dictionary, the word Similar is defined as:

has similarities in appearance, character or quantity, not being identical.

and the word Similitude as:

quality or condition similar to something.

Since arrays and pointers are not identical, but are sometimes handled similarly, we can say:

Yes, due to the array / similarity pointer, subtyping the array can be considered syntactic sugar for pointer arithmetic "

+1
source share

This may seem like an abstract or philosophical question, so for a more specific definition of what I'm looking for, this is a simple noun or noun phrase in which I could use the sentence "Yes, because of _____, subscribing to an array can be considered syntactic sugar for pointer arithmetic, "in response to, say, this question.

The phrase you are looking for is "Yes, because the array subscriber is pointer arithmetic under the hood, the array subscriber can be considered syntactic sugar for pointer arithmetic."

Which, of course, is stupid, because the reason that subscribing to an array is syntactic sugar for pointer arithmetic is that this is exactly what it is, it does not require additional explanation if the person you don’t say so know , which means subscription to an array, pointer arithmetic, or syntactic sugar. This does not mean that the compiler does not know the difference between the two (it does, because it is the one responsible for all the syntactic sugar that you get as a result).

The same question can be raised about everything that any language does for us, which we could do ourselves using more lower-level instructions and, as such, could refer to it using several terms that change only the amount of syntactic sugar used . The thing is not to pretend that they are different (they are always on the way, but they aren’t), but to simplify programming (just think about writing code without having the [] operator as syntactic sugar for pointer arithmetic and you will quickly understand the reason for your existence - this is not because it is different, because it is easier to write, read and manage).

(But please note that I am not looking for answers to this question or answers to the question “What is wrong with the word“ equivalence ”?” Yes, I know, it can mislead students to imagine that arrays and pointers "It's the same thing. I had it in mind when I wrote this entry on the list of frequently asked questions.)

Your question seems to be, “What is the modern term for array pointer equivalence in the sense that subscribing to an array is just syntactic sugar for pointer arithmetic?”

Answer:

  • "Activating an array" when you use the [] operator to index an array.
  • "Pointer arithmetic" when you manually move the pointer (as opposed to using the [] operator). It may also seem to you that you use this term for any math you do to the value that you pass to the [] operator, but you don’t actually do pointer arithmetic, but rather “calculate the index”, which is the term for the syntax sugar, which automatically takes into account the size of objects in memory that are automatically taken into account for you (which again is no different, it’s just easier to write, read and manage, this way: syntactic sugar).
+1
source share

The standard uses the term converted to. Array and pointer equivalence are still better preserved over decay array for pointers. The standard mentions this term no where, but it’s just a mutual understanding between programmers that when it comes to arrays and pointers, it is considered the equivalent of an array and a pointer. And what does the equivalent of a pointer and an array mean : pointer arithmetic and array indexing [,] are equivalent in C, pointers and arrays are different. .

0
source share

I agree that terms such as “pointer / array equivalence” or “array decay” (in the pointer) are inaccurate and misleading.

I would suggest the following:

Accessing an array using the [] operator with an index has an equivalent arithmetic pointer expression. However, there is no equivalence between the pointer and the array: I can happily change the value of the pointer (contents), perhaps to keep a separate iteration counter, but I cannot do this with a "fading" array; it is immutable and in some respects exhibits behavior similar to C ++ links.

The two verbs “disintegrate” and “transform” in English invariably indicate that the original changes in the process. Using the array name or the unary operator '&' does not change their operand.

Therefore, an expression like “returns a pointer” or “creates a pointer” will be more accurate. This is reflected in the fact that in both cases the resulting pointer is a temporary thing (usually) sitting in the GP register of the CPU. and cannot be assigned to.

At least this is my understanding, and it seems to me that a review of the showdown confirms this.

0
source share

If you are referring to type conversion, why not use the term cast ? It is generally accepted that the compiler is forced to process two related, but not the same types, as if they were the desired type. Yes, there is equivalence, but “casting” is used as a way to indicate that types are not literally the same, but that the compiler should treat the type as another type and that the procedure is acceptable for the compiler, for example up-casting for byte-> long

Second choice: "Synonym"

-3
source share

All Articles