8
#include <iomanip>
#include <iostream>
#include <Windows.h>
using namespace std;

template <class T>
void sort(int n, T a[]){
       for(i=0;i<n-1;i++){
          for(j=i;j<n;j++){
               if(a[i] > a[j]){
               temp=a[i];
               a[i]=a[j];
               a[j]=temp;
               }
           }
     }
}


void main(){
    int size;
    cout<<" Please input the amount of numbers you would like to sort"<<endl;
    cin>>size;
    int Amta[size];
    for(int i=0; i<size; i++){
        cout<<"Please enter the "<<size+1<< "number";
        cin>>Amta[i];
    }
    Sleep(100000);
}

I am trying to get the how many numbers the user would like to input from the user and store it in the variable size.

But when I initialize array Amta[size] I get the following compile errors:

Expression must have constant value

and

C2057: expected constant expression" compile error.

SHR
  • 7,940
  • 9
  • 38
  • 57
G V
  • 101
  • 1
  • 1
  • 5
  • You should look into the `new` and `delete` operators for making arrays on-the-fly. http://www.cplusplus.com/reference/new/operator%20new/ – BrainSteel Oct 24 '13 at 03:11
  • 4
    Better yet, look into `std::vector` and don't aim around your feet with a loaded gun. Also, `void main` is not legal C++. – chris Oct 24 '13 at 03:13
  • possible duplicate of [c++ array - expresssion must have a constant value](http://stackoverflow.com/questions/9219712/c-array-expresssion-must-have-a-constant-value) – i_am_jorf Oct 24 '13 at 03:15
  • 2
    Possible duplicate of [How do compilers treat variable length arrays](https://stackoverflow.com/questions/7627235/how-do-compilers-treat-variable-length-arrays) – phuclv Jul 03 '18 at 05:50

2 Answers2

14

You can't enter a non-constant value between the brackets when you declare your array:

int Amta[size];

Since you're getting size from the user, the compiler can't tell ahead of time how much memory it needs for Amta. The easiest thing to do here (especially for an exercise) is to just choose a relatively large value and make that the constant allocation, like:

int Amta[1024];

And then if you want to be careful (and you should) you can check if (size > 1024) and print an error if the user wants a size that's beyond the pre-allocated bounds.

If you want to get fancy, you can define Amta with no pre-set size, like int *Amta; and then you allocate it later with malloc:

Amta = (int *)malloc(sizeof(int) * size);

Then you must also free Amta later, when you're done with it:

free(Amta);
Aaron Golden
  • 7,092
  • 1
  • 25
  • 31
  • 10
    `malloc` in C++ is generally a terrible idea. – chris Oct 24 '13 at 03:12
  • 1
    @chris: He wants an array of `ints`. It's not like there's a constructor to be called. What's the problem? – Aaron Golden Oct 24 '13 at 03:19
  • 2
    @AaronGolden Are your saying if you (personally) were writing code that required side-by-side independent array allocations of trivially constructible objects *and* a similar array of integers, you'd use `new` for the objects and `malloc()` for the `int`s? What fun that must be. – WhozCraig Oct 24 '13 at 03:28
  • 3
    Why bother having to think about what to use based on what type you're allocating memory for? I use a `std::vector` by default. As a bonus, I don't have to clean up. – chris Oct 24 '13 at 03:29
  • 1
    Thanks went ahead and used a larger size then i would need and simply used the user input to break out of the loop when they have finished entering the final number. – G V Oct 24 '13 at 04:34
  • 1
    in C++ you should use `new` and `delete` instead of `malloc` and `free` – phuclv Oct 24 '13 at 05:08
4

C++ doesn't allow variable length arrays. The size must be a constant. C99 does support it so if you need you can use a C99 compliant compiler. Some compilers like GCC and Clang also support VLA as an extension in C++ mode

But if C++ is a must then you can use alloca (or _alloca on Windows) to allocate memory on stack and mimic the C99 variable length array behavior

Amta = (int *)alloca(sizeof(int) * size);

This way you don't need to free the memory after going out of scope because the stackframe will automatically be restored. However you need to be very careful while using this. It's still better to use std::vector in C++ for these purposes

phuclv
  • 37,963
  • 15
  • 156
  • 475