3

According to my C++ textbook, the following expression:

cout << "Enter number of students\n";
cin >> number;
int score [number];

Is an ILLEGAL expression. I cannot use a variable for the array size.

Why can't I do this? (I'm not looking for an alternative, I haven't gotten into pointers, vectors etc, but I want to understand this behavior.)

user2864740
  • 60,010
  • 15
  • 145
  • 220
Dimensions
  • 71
  • 1
  • 7

2 Answers2

5

Variable length arrays were not supported in ISO C90/ANSI C89 from which C++ is derived. While VLAs were added in C99 which deviates from C++, they are arguably unnecessary in C++ which has STL container classes to provide more flexible methods of storing multiple objects.

Clifford
  • 88,407
  • 13
  • 85
  • 165
  • 1
    On the other hand, regarding "they are arguably unnecessary in C++", they are also arguably *useful* in C++ since we have an active proposal and -- almost nothing at all is absolutely necessary. – Cheers and hth. - Alf Sep 29 '14 at 03:56
  • 2
    @Cheersandhth.-Alf: That the thing about "arguable" - you can argue about it. I would not say "nothing at all is absolutely necessary", certain features in a programming language are necessary to make it "Turing Complete". It is proposed, but no one on the ISO committee seems in a hurry. – Clifford Sep 29 '14 at 09:01
0

In C++, the compiler must know the amount of memory to allocate for an array at compile time. However, the value of a variable is not known until run time. This is why you are not allowed to use a variable for the size of an array.

If you are required to use arrays for a class project, I suggest using a const to define the maximum size that you will allow. Later on, you will learn how to user other techniques such as pointers and STL containers (such as std::vector).

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • 1
    Doesn't actually answer the question. Also, no mention of vectors. – Neil Kirk Sep 29 '14 at 03:07
  • Note that this is an antique style, since 1998 it is preferred to write `std::vector number;` – M.M Sep 29 '14 at 03:07
  • 1
    @NeilKirk Technically you're right that it doesn't answer the question, but it does provide an alternative for what the OP wants to achieve – M.M Sep 29 '14 at 03:08
  • 2
    Not what I was looking for. I'm not looking for an alternative, I haven't gotten into pointers, vectors etc. I want to understand. – Dimensions Sep 29 '14 at 03:11
  • @MattMcNabb That might be acceptable if the question were "how can I have a dynamically sized array in C++". But in this case, the OP is clearly aware of the restriction and likely how to get around it, it's a *why* question. For that reason I find it hard to believe that this answer does anything to lead the asker on the path towards the ultimate answer. – Chris Hayes Sep 29 '14 at 03:12
  • @Dimensions The code you are trying to use doesn't compile, so you must use **some** alternative. – Code-Apprentice Sep 29 '14 at 03:12
  • Unfortunately it does not explain why it is not allowed, other than the rules say not. – Neil Kirk Sep 29 '14 at 03:15
  • @Code-Apprentice : You may have been better off adding a new answer and deleting this one rather than rendering all the comments moot. – Clifford Sep 29 '14 at 03:17
  • @NeilKirk If there is more to it than what I have said here, feel free to provide your own answer. – Code-Apprentice Sep 29 '14 at 03:17