I always thought that I knew what an array element was, but now I’m not sure. As we know, a classic fixed-size array consists of a set of cells. For example:

int a[10] - массив из 10 ячеек типа int (каждая по sizeof(int) байт)

An array cell is a memory area that we can access by index. Its position in the array can not be changed. Each cell stores the value (in our example, a scalar).

Question: is an array element a synonym for a cell (memory area) or a synonym for a cell value? For example, is the statement true: "elements of an array can be swapped"?

Interested in the answer in general and the answer for C ++ in particular.

  • I myself was looking for definitions in nete and by books. It seems that the expression "array element" is often used according to circumstances: either as a cell, or as a value written in a cell - alphard
  • one
    I think that theoretically an element of the array is still a memory area, but in the "everyday life" the values ​​of the array are also denoted by the values ​​themselves, which in general is not entirely true. - cy6erGn0m
  • one
    Something the people today are drawn to the philosophy. Actually, what I wanted to say, the fact of the matter is that the array elements cannot be interchanged (only values), since the position of the i-th value is always calculated as offset (sizeof (type) * i) from the beginning (the pointer to which we have the name variable). - Dex
  • Well, the address is in C and C ++. In other languages, it can be done as you please =) But I think it will be impossible to change it anyway, well, if you are just in the interpreter, and even then, you can patch it up to perform such a strange operation. = D - Alexey Sonkin
  • one
    Obviously, the question of the classic "raw memory" arrays. I understand that in some thread a java array can be implemented, say, by a simply linked list - alphard

4 answers 4

In essence, an array element is a separate variable. And the definition and meaning can be taken for it:

Variable - a named, or otherwise addressable area of ​​memory whose name or address can be used to access data in a variable (at a given address).

Here it is. That is, formally, this is a memory area. Cell.

But in spoken language, this value is only partially observed. Apparently, many (like me) do not make a significant difference between these concepts, but someone does not even suspect it.

When they talk about array operations as such - “create an array of several elements”, “add an element”, “delete an element” - we can assume that they mean operations on cells.

Well, not in the sense that they are created or deleted, but in the fact that they allocate or free memory. Since the values ​​there, in fact, do not play any role.

When it comes to processing algorithms, the value comes to the fore. And the phrase "swap elements of an array" in the context of some algorithm, for example, sorting or encryption will be clear and true. Or "find the maximum / minimum element" - here it is also clear that what is meant is not their physical location in memory or the index. =)

  • The last few paragraphs are somehow very long. but by and large, perhaps I will accept such an answer, thank you - alphard
  • The last paragraphs are my thoughts on the subject. The answer is at the beginning. Please =) - Alexey Sonkin

There is a model - an array and its elements, but there is a realization: for example, in C - these are pointers and address arithmetic. "Swap elements" is something like a model, an abstraction, a problem statement. And the assignment of values ​​to the cells at certain addresses is the implementation.

  • here the question was posed quite specifically: what does "element" mean in the context of the standard implementation, which I described - alphard

The array element can be an object .. In the case of associative arrays

 return { object: { name: "First", dest: "Just for fun" }, object_2: { name: "Second", dest: "Just for fun" } }; 

something like this))

if you think that everything is relative, then you can specify)) and not abstract

    Question: is an array element a synonym for a cell (memory area) or a synonym for a cell value? For example, is the statement true: "elements of an array can be swapped"?

    An array, in the conventional sense, is a set of values ​​(elements) located in cells. Each cell is indexed. As a rule, we do not touch the indices, but work with the contents. Therefore, the statement “elements of an array can be swapped” is true, and it corresponds to the exchange of the contents of the “cells”.

    The statement "array elements can be swapped" may (but is not required) not correspond to the exchange of contents if we have introduced our own indexing mechanisms (and in fact addressing), for example, have overloaded the operator []. This is an infrequent case, rather an exception to the rules, which should be negotiated.