When we declare a variable we are reserving a memory space for it, nothing else. And the line int vetor [];
is just the statement, nothing more.
By obvious the syntax of the declaration of a array requires you to know what kind of data it will contain, and this has, we know it’s a int
and that it’s probably 4 bytes in size (it’s a little more complicated than this, but that’s not the case here), and we need to know how many elements you’ll have in the array to multiply by 4 and know how many bytes need to be allocated. Where is this absolutely necessary information? There’s nothing you can do to fix this.
In the example that works it has the size 1, which multiplied by 4 indicates that the variable will have 4 bytes reserved in memory. All you need to know the size you will allocate.
You can even set the size when allocating through a variable, but you can’t be left with nothing.
It is possible that the compiler will even discover the size by itself depending on the syntax:
int vetor[] = { 1, 2, 3 };
I put in the Github for future reference.
Thus vetor
will have size 3, and will reserve 12 bytes in memory for this variable, and will automatically put the values above.
C++ . pq when I leave without setting it does not work.
– Elder Son
Enter the code and error.
– Maniero
error: Storage size of ːvetor' isn’t known|
– Elder Son
ready edited... the only doubt is to understand this
– Elder Son
If you translate the error, you might better understand the error.... "the 'vector' storage size is not known"
– MarceloBoni
The problem has already been solved. I understood by the explanation given by bigown . It was only to know the importance of having an index that defines the size of the vector. Valeu ae!
– Elder Son