0

I'm trying to have the user input the size of the array. I use the scanf function to have the user input an integer and store that number to a variable "wishlist" and then use "wishlist" as the size of my array.

'''

    //prompt user for input
    scanf("%d", &wishlist);
    
    double itemCost[wishlist];
    int itemValue[wishlist];

'''

I keep getting the error message "expression must have constant value". Some other things I've tried is declaring the array before the scanf, after the scanf, even initializing the arrays with a value of {0}. Anything I've tried generates the same error message. Using an online IDE I've been able to do this just fine. But whenever I try and replicate it within Visual Studio for my assignment, it doesn't work. Any suggestions?

yano
  • 4,827
  • 2
  • 23
  • 35
  • perhaps useful: https://stackoverflow.com/questions/58462881/how-to-use-visual-studio-as-an-ide-with-variable-length-arrayvla-working – yano Jun 15 '22 at 14:58
  • Short story: Visual Studio does not support _variable length arrays_ (VLAs) like `double itemCost[wishlist];` and `int itemValue[wishlist];`. Using Visual Studio for C requires that you set it up propery. I'm not sure if there's even a C project template and if you start coding in C++ mode, you are compiling your "C" code with a different set of rules than you'd expect. C++ != C – Ted Lyngmo Jun 15 '22 at 14:59

0 Answers0