C++ Array - Expression Must Have a Constant Value

Expression must have a constant value in C

You must compile the code with a standard C compiler, such as gcc or clang etc. You can't compile using C++ compilers or non-C-compilers such as Microsoft VS.

Other than that, the code is fine apart from missing a return statement.

If you are stuck with old crap compilers, you can alternatively do an old style "mangled array":

size_t m = strlen(something) + 1;
size_t n = strlen(something) + 1;
...
char* b = malloc(m*n);
...
b[i*n + j] = something;
...
free(b);

That is how we used to allocate 2D arrays dynamically back in the old days.

Equivalent code using dynamic allocation in modern standard C would be:

char (*b)[n] = malloc( sizeof(char[m][n]));
...
b[i][j] = something;
...
free(b);

expression must have a constant value error in VS code for C

C supports variable length arrays since C99, so your array declaration is valid.
Your IDE might be set to C++ mode or to a C standard before C99 and therefore throw that warning.

On a side note, you did not initialize your rows and columns variables or the array content, meaning their values are undefined.

C++: Expression must have a constant value when declaring array inside function

There is a misconception here with what const means, probably because it's a little confusing that this works:

const int SIZE = 20;
int array[SIZE];

but this doesn't:

void foo(const int SIZE) {
int array[SIZE];
// ...
}

const int SIZE = 20;
foo(SIZE);

The issue is that the array size in an array declaration must be a core constant expression. Simplified, that means an expression that's evaluatable at compile time to be a constant. That is true in the first case (you can see that SIZE is the integral constant 20) but that is not true in the second case. There, the SIZE function parameter is just const - in the sense that it is nonmodifiable - and not a core constant expression. You can see the difference in that I can call foo() with something that is clearly unknowable until runtime:

int x;
if (std::cin >> x) {
foo(x);
}

In order to pass an argument into foo, and have that argument be used as an array bound, it is not enough to have it be const - the actual integral value must be encoded into the type (unless you call foo() as constexpr which I'm assuming is not the case here). In which case, you'd have to do something like:

template <int SIZE>
void foo() { ... }

const int SIZE = 20;
foo<SIZE>();

or:

template <int SIZE>
void foo(std::integral_constant<int, SIZE > ) { ... }

const int SIZE = 20;
foo(std::integral_constant<int, SIZE>{} );

or simply have SIZE be a global constant or otherwise accessible to foo() in a way that doesn't have to do with its arguments.


Or, there's always the simple option: use std::vector:

void foo(const int SIZE) {
std::vector<int> v(SIZE);
...
}

Array definition - Expression must have a constant value

In C language keyword const has nothing to do with constants. In C language, by definition the term "constant" refers to literal values and enum constants. This is what you have to use if you really need a constant: either use a literal value (define a macro to give your constant a name), or use a enum constant.

(Read here for more details: Shall I prefer constants over defines?)

Also, in C99 and later versions of the language it possible to use non-constant values as array sizes for local arrays. That means that your code should compile in modern C even though your size is not a constant. But you are apparently using an older compiler, so in your case

#define SIZE 10

is the right way to go.

C error: expression must have a constant value

Since they are defined as constants, what have I overlooked?

In C objects declared with the const modifier aren't true constants. A better name for const would probably be readonly - what it really means is that the compiler won't let you change it. And you need true constants to initialize objects with static storage (I suspect regs_to_read is global).

You could try assigning regs_to_read in a function called before anything else uses that array.



Related Topics



Leave a reply



Submit