C++ Expression Must Have a Constant Value

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.

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);

error expression must have a constant value

You can't initialize a variable of static storage duration with anything but a constant expression, an integer constant expression in this case.

Unfortunately, C makes a difference between integer constant expressions and "const qualified" variables. (C and C++ are different here.) This means that the initializer of your cfg variable must be an integer constant (for example 1), an enum or a #define value, or an expression formed by such operands.

That is, if you do any arithmetic inside the initializer, all operands must be integer constants.

So 1 + 1 would be fine, but not a + 1, if a is a variable.

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 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.

(C++) Expression must have a constant value

Episode* episodes[count] is not valid because count is a parameter from the function
loadEpisodesFromFile which is unknown at compiling time...

you need to use an std::vector instead

std::vector<Episode*> episodes(count);

VScode expression must have a constant value in c99 mode

The IDE may be warning you of a potential programming error, since value N can be changed while mintree and treewalked are still in scope. You should be able to fix this with the following modification:

#include <stdio.h>
#include <stdlib.h>

int main() {
int N_scan, i, j;
scanf("%d", &N_scan);
const int N = N_scan;
int min_tree[N][N];
int tree_walked[N];
}

The value of N cannot be changed while min_tree and tree_walked are in scope.

There is a similar answer (for a C++ case) here:

https://stackoverflow.com/questions/9219712/c-array-expression-must-have-a-constant-value#:~:text=expression%20must%20have%20a%20constant%20value.%20When%20creating,declared%20const%3A%20doesn%27t%20even%20provide%20a%20variable%20name.



Related Topics



Leave a reply



Submit