+ 5

# Using arrays in c++.

I'm learning c++, vectors to be more specific, what is the difference between this: int array[]; int array[5]; the first array with empty brackets accepts any elements? and the second array accepts 5 elements? what is the difference?

12th Jul 2018, 11:34 PM
Eduardo Perez Regin
9 Réponses
+ 6
int array[5] specifies the size to be five. int array[] says that the compiler can figure the size out. With a line like this: int array[]; The compiler can't figure out the size, so it errors. A line like this however will work: int array[5]; The compiler allocates memory for 5 int's in the array; This line allows the compiler to figure out the size (Which is 3 in this case): int array[] = {1, 2, 3}; That line is the same as this: int array[3] = {1, 2, 3}; Hope that helped! :)
12th Jul 2018, 11:43 PM
Blake Quake
+ 5
Eduardo Perez Regin try putting the first array in a c++ code. int array[]; ^^^ See what you get.☺
12th Jul 2018, 11:37 PM
Manual
+ 4
Eduardo Perez Regin In case of vector which is the dynamic version of the ordinary array, most of the time, depends on the purpose of the container, you just declare the vector and leave the vector size, content, reserved storage, etc. as is by default without any explicit definition. This is something that you can't do with normal arrays. Let me give you a simple example vector<int> vec; in this declaration, the vec's size and capacity is set to 0 by default. That means the vector is full empty and ready to accept integer elements during the execution of the program using JUST push_pack() method. vec.reserve(8); Later on or immediately after vec's declaration, you can set the required storage for the vec as above*. vec[0] = 10; // 10 vec.at(1) = 20; // 10 20 vec.insert(vec.begin()+2, 30); // 10 20 30 vec.push_back(40); // 10 20 30 40 If you are willing to declare the vector as vector<int> vec(8); then you effectively prepared 8 empty cell of storage to work with. Thus you are able to directly
13th Jul 2018, 7:07 AM
Babak
+ 4
Cont. access each element of the container. So, you are eligible to fill the vector's cells using all the above methods. ____ * Be careful when reserving the storage since exceeding the reserved value causes storage reallocation and invalidates all references to the vector elements.
13th Jul 2018, 7:10 AM
Babak
+ 3
The second is fine, the first is in complete. The debugger will tell you what you need.
12th Jul 2018, 11:39 PM
Manual
+ 3
Manual and Blake Quake are correct, but these are not vectors. Do you still wish to use vectors, or did you only want to know about array syntax?
13th Jul 2018, 2:40 AM
Zeke Williams
0
Your first declaration is invalid and would give a compiler error, for a dynamic array you'd use "std::vector<type> vectorName"
13th Jul 2018, 4:07 PM
Noah
0
the first line is incomplete as the array is not completely defined. the second line of code allocates menory for 5 integers in the array
14th Jul 2018, 4:48 PM
Zellie Y Thomas
0
if we decler de size its better so its a
18th Jul 2018, 9:33 AM
nastaran rahimi
Aujourd'hui en vedette
Functions