Question:
Why should I use a constant variable to declare the size of an array in c++?
Damien Bell
2011-05-07 10:37:21 UTC
I've always wondered this but I've never really been able to google a decent answer that doesn't go into 50 pages of explanations about pointers and DMA.
Three answers:
?
2011-05-07 13:39:28 UTC
The syntax for array declaration in C++ is

     T D [constant-expression]

This is different from C, where arrays can have variable length, because C++ has many other containers that are variable-length, and the purpose of arrays in C++ was strictly as containers whose size cannot change during lifetime ("was" but not anymore, since std::array has been around for a decade. Arrays are for legacy code only.)



If you need a runtime-sized container, use vectors or whatever else is suitable

     int size;

     std::cin >> size;

     // int a[size]; // ok in C since 1999, error in C++

     std::vector a(size); // ok in C++

As the other answer points out, it is also possible, although not advised, to use operator new[] to create arrays with dynamic storage duration:

     int* p = new int[size];

     delete[] p;
cerchia
2016-12-17 08:41:55 UTC
There are 2 varieties of memory i7b8b965ad4bca0e41ab51de7b31363a1 a working laptop or laptop: Dy7b8b965ad4bca0e41ab51de7b31363a1amic a7b8b965ad4bca0e41ab51de7b31363a1d static. Static is far less complicated to get entry to, whether, it ca7b8b965ad4bca0e41ab51de7b31363a1 o7b8b965ad4bca0e41ab51de7b31363a1ly handle co7b8b965ad4bca0e41ab51de7b31363a1sta7b8b965ad4bca0e41ab51de7b31363a1t values for array sizes, so which you may the two would desire to swap n for a 7b8b965ad4bca0e41ab51de7b31363a1umber, or positioned a preprocessor comma7b8b965ad4bca0e41ab51de7b31363a1d defi7b8b965ad4bca0e41ab51de7b31363a1i7b8b965ad4bca0e41ab51de7b31363a1g n as some co7b8b965ad4bca0e41ab51de7b31363a1sta7b8b965ad4bca0e41ab51de7b31363a1t "#defi7b8b965ad4bca0e41ab51de7b31363a1e n=one hundred" To get entry to dy7b8b965ad4bca0e41ab51de7b31363a1amic memory, which accepts variables as array sizes, you're able to i7b8b965ad4bca0e41ab51de7b31363a1itialize it as show7b8b965ad4bca0e41ab51de7b31363a1: 7b8b965ad4bca0e41ab51de7b31363a1ode *A = 7b8b965ad4bca0e41ab51de7b31363a1ew 7b8b965ad4bca0e41ab51de7b31363a1ode*[n]; or whichever form you wa7b8b965ad4bca0e41ab51de7b31363a1t to apply replaci7b8b965ad4bca0e41ab51de7b31363a1g 7b8b965ad4bca0e41ab51de7b31363a1ode The7b8b965ad4bca0e41ab51de7b31363a1 you're able to derefere7b8b965ad4bca0e41ab51de7b31363a1ce this poi7b8b965ad4bca0e41ab51de7b31363a1ter each and every time you desire to get entry to the array; the memory ca7b8b965ad4bca0e41ab51de7b31363a17b8b965ad4bca0e41ab51de7b31363a1ot be moved. wish i helped :) Luke
Whiplash
2011-05-07 11:29:05 UTC
Who says you have to use a constant variable? If you dynamically create them then you can make them any size you want. Such as:



int size;

cout << "Enter size of array: ";

cin >> size;

int *array;

array = new int[size];



If your going to just use "'Int name[size]; That will give you an error. The new operator allows a program to create arrays during runtime.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...