1

Look at code below:

#include <iostream>
int main(void) {
    int  number1 = 4;
    double salary[number1];
    return 0;
}

Though it can be successfully compiled, it should be considered wrong because number1 is non const variable.

#include <iostream>

int main(void) {
    int  number1 = 4;

    const unsigned number2 = number1;
    double salary[number2];

    return 0;
}

Now the number2 is a const variable.

Also compiled successfully.

I feel it is wrong or at least not good practice.

But I can't explain why.

Can someone explain why it is wrong ?

Nikos C.
  • 50,738
  • 9
  • 71
  • 96
  • 1
    Neither of those will compile in a standard conforming compiler. – Timo Mar 15 '20 at 23:45
  • 1
    C and C++ are compiled languages to the underlying physical architecture of the system. Types and qualifiers like `int` and `const` (esp. the latter) are created more to help human programmers. So if you feel it doesn't express the meaning you intend, then by all means don't use that syntax. In that sense, it could be considered 'bad practice' Side note: consider the function `alloca`. – C. Dunn Mar 15 '20 at 23:47
  • 1
    I feel this is a perfect dupe https://stackoverflow.com/a/34696833/2805305 , except for the fact that this q also asks about `C`. Nevertheless read my answer there as it is perfectly on point regarding your question. – bolov Mar 16 '20 at 00:00
  • 3
    In the future, please ask different questions (incl the same question as it pertains to different languages) in separate Questions. – ikegami Mar 16 '20 at 00:01
  • 1
    @Timo, you are only partially correct -- C99 allows variable length arrays. – Armen Michaeli Mar 16 '20 at 00:08
  • 2
    C and C++ are different languages and both have their own distinct specifications which have evolved over time -- depending on the language and specification, an array like you want may be defined or it may not. Also, just because you feel it is wrong it doesn't make it bad practice -- some hardware may be just the right fit for the kind of construct that feels "wrong" to you. So noone can explain why it is wrong, because it arguably is not [wrong]. Converse is also true -- it isn't "right" -- it just may be allowed, and implications need to be assessed on a case by case basis. – Armen Michaeli Mar 16 '20 at 00:11
  • Use the `-pedantic-errors` compiler flag to force the compiler to give error messages about programs that would be ill-formed according to the C or C++ standard if you are using GCC or Clang. For MSVC use the `/permissive-` flag. Without these none of the compilers try to be completely standard-conform and instead provide compiler-specific language extensions. – walnut Mar 16 '20 at 00:14
  • Only ask questions about C or C++. As you can see, the current accepted answer primarily answers the C++ part. Ask the question separately for C or C++. I'd personally edit the question and remove C part and tag and re-ask it for C. – ljrk Mar 16 '20 at 09:15

2 Answers2

11

Can a const variable which can change be used to declare and define an array in ... C++?

No.

Though it can be successfully compiled

... by a compiler that extends the language. There is no guarantee that it can be compiled by other compilers.

because number1 is non const variable.

Sort of, indirectly. The exact reason is that number1 is not a constant expression. Making the variable const would be sufficient in this case to make the example well-formed because the initialiser of the variable is a constant expression.

Now the number2 is a const variable.

Also compiled successfully.

I feel it is wrong

Your feelings are correct. The program is still ill-formed. As a standard conforming compiler is required to tell you (you can ask GCC to conform to the standard by using the -pedantic option):

warning: ISO C++ forbids variable length array 'salary' [-Wvla]
double salary[number2];
       ^~~~~~

Can someone explain why it is wrong ?

It is wrong because the size of the array is not a constant expression, as required by the language.

The identifier of a const variable is not necessarily a constant expression. The variable must be const, and the initialiser of the variable must be a constant expression. 4 is a constant expression. Identifier of a non-const variable is not a constant expression. If the value is determined at runtime, then it cannot be a constant expression.


Can a const variable which can change be used to declare and define an array in C ...?

Potentially, yes. Before C99, no for same reasons as described in the C++ answer.

In C99, yes because variable length arrays were introduced to the language. Since C11, yes if the compiler supports it; VLA were made optional in this standard.

Community
  • 1
  • 1
eerorika
  • 232,697
  • 12
  • 197
  • 326
3

Can a const variable which can change be used to declare and define [size of] an array in C and C++?

In C programming language a variable length array is a declaration of an array of variable size. The expression inside braces int array[here] is not a constant expression and is evaluated at runtime (and must always evaluate to a value greater than zero). It's part of C99 standard.

VLA is not a part of C++ and is invalid in C++, but some compilers support variable length arrays also in C++ as a compiler extension.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111