200k views
5 votes
Which of the following declarations is illegal?

a) int a = 0, b = 1, c = 2; int array[3] = (a, b, c);
b) int size = 3; int array[size];
c) int size = 3; int array[size] = (1, 2, 3);
d) All declarations are legal.

1 Answer

4 votes

Final answer:

The illegal declarations are (b) int size = 3; int array[size]; and (c) int size = 3; int array[size] = (1, 2, 3);

Step-by-step explanation:

The illegal declarations are (b) int size = 3; int array[size]; and (c) int size = 3; int array[size] = (1, 2, 3);

  • In declaration (b), the size of the array is determined by the variable 'size', which is not a constant. Array sizes must be constants in C++.
  • In declaration (c), the syntax used to initialize the array is incorrect. To initialize an array, each value should be enclosed in curly braces {}.

Therefore, the correct answer is (d) All declarations are legal.

User Laodao
by
7.5k points