I'm confused about arrays. When you declare them is like having 1 variable and many data or many variable and one data type?
-
It's like many variables. A constructor and a destructor is called for each element. – Thomas Sablik Mar 30 '20 at 11:50
-
@ThomasSablik if a class has members and their constructors and destructors are called you wouldnt call `someType x;` "many variables" for that reason – 463035818_is_not_an_ai Mar 30 '20 at 11:53
-
it's apparent in the question that there's a massive confusion of terminology anyway. – tenfour Mar 30 '20 at 11:53
-
@tenfour I don't see how, to be honest. The question seems reasonable, as do the two hypothetical options, though perhaps not particularly useful. – Asteroids With Wings Mar 30 '20 at 12:10
-
@idclev463035818 How is "I don't understand this central concept in a major programming language" off-topic on SO? – bitmask Mar 30 '20 at 18:01
-
@bitmask I first did not understand the question. Thats why I didnt vote, but left a comment. I can now remove that comment, repeating the still useful link here: https://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c/4810668#4810668 – 463035818_is_not_an_ai Mar 30 '20 at 18:11
-
Hi, my question is if structures are the opposite of arrays? while an array is a collection of the same data type, are structures still a collection, but of different data types? since structures and classes in c++ are both equal, are classes, a collection of different data types? thanks this is another question that I made that explain better my problem @idclev463035818 – Wildzaka02 Mar 30 '20 at 18:42
3 Answers
This is very interesting question, even though it sounds a little bit unclear. To answer this question properly, we first need to understand all the different terms you used in your question.
What is a variable?
From Wikipedia:
In computer programming, a variable or scalar is a storage address (identified by a memory address) paired with an associated symbolic name, which contains some known or unknown quantity of information referred to as a value.
It simple terms, a variable is a dedicated space in RAM with a symbolic name. By that simple definition a C++ array is a variable. In C++ you define an array by giving it a name, a data type and the size.
Another point worth noting:
Compilers have to replace variables' symbolic names with the actual addresses of the data.
This means that the names of the variables are not part of the data stored in memory.
What is data?
Again, from Wikipedia:
Data (treated as singular, plural, or as a mass noun) is any sequence of one or more symbols given meaning by specific act(s) of interpretation.
(...)
A program is a set of data that consists of a series of coded software instructions to control the operation of a computer or other machine. Physical computer memory elements consist of an address and a byte/word of data storage.
(...)
Data can be organized in many different types of data structures, including arrays, graphs, and objects. Data structures can store data of many different types, including numbers, strings and even other data structures.
From this we learn learn few key concepts:
- An array is a type of data structure.
- Data structures can store data or other data structures.
- The word data can be used as both singular and plural noun as well as mass noun. In computing we treat data as a mass noun - something that cannot be counted.*
This means that arrays are data structures, which can store data including other arrays.
What is data structure? What kind of data structure is an array?
From Wikipedia:
An array is a number of elements in a specific order, typically all of the same type (depending on the language, individual elements may either all be forced to be the same type, or may be of almost any type). Elements are accessed using an integer index to specify which element is required. Typical implementations allocate contiguous memory words for the elements of arrays (but this is not always a necessity). Arrays may be fixed-length or resizable.
This is a simple definition of what an array is in computer science, however, it is a little bit different in every language and the exact definition depends on a lot of factors. For example, "an array in PHP is actually an ordered map."1
When it comes to C++ definition of an array, a great explanation is given by cplusplus.com:
An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier.
That means that, for example, five values of type int can be declared as an array without having to declare 5 different variables (each with its own identifier). Instead, using an array, the five int values are stored in contiguous memory locations, and all five can be accessed using the same identifier, with the proper index.
Conclusion
In C++ when you declare an array you actually declare a single variable, which can store one or more values of the same data type.
When we use this syntax:
int foo [5] = { 16, 2, 77, 40, 12071 };
It can be read out loud as "we are defining a variable, which is an array of five integers".
* I would argue we should not say "many data" in computer science as explained by this post: Is it okay to say “I wrote this article after reading many data”?

- 30,962
- 25
- 85
- 135
An array variable is one variable; it is not many variables.

- 232,697
- 12
- 197
- 326
-
-
1@Wildzaka02 `hello` is the variable, its type is `int[50]` – 463035818_is_not_an_ai Mar 30 '20 at 11:55
-
@Wildzaka02 already in the question you seem to confuse "data" with "data type"... the elements are all of same type, but of course the can hold different values – 463035818_is_not_an_ai Mar 30 '20 at 11:56
-
@idclev463035818 I don't see that as a "confusion". The OP is describing homogeneity, a pretty standard programming concept. – Asteroids With Wings Mar 30 '20 at 12:10
It depends on what you mean by "variable", as this isn't a term that C++ defines.
Conventionally, I suppose we deem a "variable" to be "an object described by a name". In that sense, a declared array is one variable.
But more strictly, an array is an object, that contains N other objects. This is like how your class objects can contain other objects (members, or "sub-objects"), or like how the United Kingdom is a country that consists of other countries. So, the answer to your question could be: "both".
I see little value in dissecting the terminology more than that, to be honest; more important is how you use the array.

- 17,071
- 2
- 21
- 35
-
well i need understanding better oop... apparantly structures,arrays and objects are the same.... but in your answer you said that the array name is a variable with other variables? for example (int a[50]) "a " is the variable that contains other 60 variables? – Wildzaka02 Mar 30 '20 at 12:27
-
@Wildzaka02 It's up to you and the people you are talking to, to come up with a set of terminology that works for you. The standard doesn't talk about this, so it's up to you. I'm curious why it matters so much? This is a triviality. – Asteroids With Wings Mar 30 '20 at 12:55
-
@Wildzaka02 _"apparantly structures,arrays and objects are the same"_ Not sure what you mean by this. In what manner are they "the same"? – Asteroids With Wings Mar 30 '20 at 12:56
-
for example:"struct a{ int s; string b; char l; }object; ;" in this case object became the array with size of for types" string b,char l,int s" and it's like a heterogeneous array where we first declare all the types that the "object " must have where it became the array ,and when we write something like "object.a" the ".a" is the index like object[0];("if it was homogeneous array) – Wildzaka02 Mar 30 '20 at 13:29
-
@Wildzaka02 I wouldn't describe it that way; an array is a specific kind of thing. Nobody would call a class object an array. Though, yes, it is "like" a heterogeneous array in some ways (but not in all ways! consider padding and member re-ordering for example) – Asteroids With Wings Mar 30 '20 at 16:58
-
-
if structures are the opposite of arrays? while an array is a collection of the same data type, are structures still a collection, but of different data types? since structures and classes in c++ are both equal, are classes, a collection of different data types? – Wildzaka02 Mar 30 '20 at 20:43
-
@Wildzaka02 These are all separate questions. I would advise to keep the discussion on one single topic only. If you have a question about objects and structures it would be a new question. – Dharman Mar 30 '20 at 20:49
-
@Dharman I meant to say if objects are collectors of data like arrays? – Wildzaka02 Mar 30 '20 at 21:08
-
@Wildzaka02 No, objects are something completely different. They are instances of particular class with or without data. – Dharman Mar 30 '20 at 21:12
-
@Wildzaka02 _"I meant to say if objects are collectors of data like arrays"_ Yes. – Asteroids With Wings Mar 30 '20 at 23:11
-
@Wildzaka02 _"if structures are the opposite of arrays?"_ No, they're not opposite either. They're different things. You're going to drive yourself crazy trying to draw these arbitrary comparisons. There's no point to it. – Asteroids With Wings Mar 30 '20 at 23:12
-
Asteroids With Wings yeah I became crazy at this point I've started to make comparison of everything... – Wildzaka02 Mar 31 '20 at 07:43