C Language Compilation Error: variably modified ‘* *’ at file scope

Error: variably modified ‘* *’ at file scope

Cause of error

Read only type used in array declaration.

This error is caused by the use of code similar to the following

const int length = 256;
char buffer[length] = {0};

In C language, const is not a real constant, its meaning is only read-only. The object declared with const is a runtime object and cannot be used as the initial value of a quantity, the length of an array, the value of a case, or in the case of a type. for example

//Error message in the comment
const int length = 256;
char buzzer[length];        //error: variably modified ‘buffer’ at file scope
int i = length;             //error: initializer element is not constant

switch (x) {
case length:            //error: case label does not reduce to an integer constant
	/* code */
	 break;
default:
	 break;
}

The solution is to use the macro definition instead of the read-only type const

//how to solve this error
#define LENGTH 256
char buzzer[LENGTH];        //error: variably modified ‘buffer’ at file scope
int i = LENGTH;             //error: initializer element is not constant

switch (x) {
case length:            //error: case label does not reduce to an integer constant
	/* code */
	 break;
default:
	 break;

#The difference between define and const
the type modified by const takes up space in memory, while # define does not. # define only replaces the corresponding part of the source file to be compiled with string before compilation. For example, the previous code will be preprocessed to

char buzzer[256];       
int i = 256;            

switch (x) {
case 256:           
	/* code */
	 break;
default:
	 break;

Read More: