Value placement NOT constant in fixes

2

Good evening, I try to create a C ++ program that combines arrays in for and switch; however, I mark error in the declaration of the variable numbers [p], I know that it is because the number between the brackets must be a constant. My question is, is there any way to declare a constant NO value within the brackets? In advance, thank you.

#include <iostream>

int n,p,c,numero;
int numeros[p];

using namespace std;

{
    void CrearArreglo()
{
    {
        cout<<"Inserta el numero de indices que quieres que tenga tu arreglo (entre 5 y 20)"<<endl;
        cin>>n;
    }
}
void LlenarArreglo()
{
    int numeros[p];
    for(int p=0;p<n;p++)
    {
        cout<<"Introduce un numero"<<endl;
        cin>>numero;
        numeros[p]=numero;
    }
}
int main()
{
    do{
    cout<<"Elige una opcion"<<endl;
    cout<<"0 Salir"<<endl;
    cout<<"1 Crear arreglo"<<endl;
    cout<<"2 Llenar arreglo"<<endl;
    cin>>c;
    switch(c)
   {
        case 1:
               {
                   CrearArreglo();
               }
        break;
        case 2:
               {
                   LlenarArreglo();
               }
        break;
}
}while(c!=0);
}
}
    
asked by dannaobelana 14.11.2017 в 04:15
source

2 answers

5
  

Is there a way to declare a constant NO value inside the brackets?

Assuming you refer to using a non-constant value (constant values are not declared inside brackets ), you can do it in two situations:

1.- Arrangements of dynamic size (AtD).

Known as Variable length array in English are not part of the C ++ standard but some compilers support them as extension to the compiler. If this extension is activated, a non-constant value can be used to define the size of an array:

int p;
std::cin >> p;
int numeros[p]; /* Valido si la extension AtD existe y esta activada.
                   no valido en caso contrario! */

Since this option is dependent on the compiler, it turns out not to be portable and therefore its use is discouraged.

p>

2.- Arrangements defined at runtime.

If you reserve the memory dynamically, it is possible to use a variable for the size of the array:

int p;
std::cin >> p;
int *numeros new int[p];
//                   ^ <-- esto no es una constante!

Do not forget to erase the memory when you no longer need it:

delete []numeros;

Or better, use smart pointers:

int p;
std::cin >> p;
auto numeros = std::make_unique<int[]>(p);
//       esto no es una constante! --> ^

for (int i = 0; i < p; ++i)
    numeros[i] = i; // Se usa como un arreglo normal
    
answered by 14.11.2017 / 11:21
source
6

Your question is:

  

is there any way to declare a constant NO value within the   square brackets?

And the answer is NO .

The dimension of a array must be a constant of known value when compiling, obligatorily. To reserve a free memory portion defined during execution, you must use new (or some appropriate container from the standard library).

Comment: Well, do not take this NO in an exhaustive way; there is some compiler that accepts it as an extension to the language.

    
answered by 14.11.2017 в 05:30