How to make a compilation successful for a program with a variable length array?(currently, Showing error : Variable sized array). I am using gcc in linux. How to make compiler compatible to c99 standard ? PLease help me in this. THanks in advance.
-
1With a new enough version of GCC it will automatically use a later version of the C standard. Which version are you using? What are you trying to do? Please edit your question to include a minimal reproducible example of your code, together with a full and complete copy-paste (as text) of your errors (with comments added in the code where you get the errors).Some programmer dude– Some programmer dude2020-07-06 04:44:45 +00:00Commented Jul 6, 2020 at 4:44
-
Also please take some time to read the help pages, take the SO tour, read How to Ask, as well as this question checklist.Some programmer dude– Some programmer dude2020-07-06 04:45:10 +00:00Commented Jul 6, 2020 at 4:45
-
3it would have been faster to read the diagnostic message that tells you what to do, than write this question ...M.M– M.M2020-07-06 04:45:43 +00:00Commented Jul 6, 2020 at 4:45
-
@M.M Usually a VLA warning is just to hint that one uses VLAs in their code. It does not tell you what to do to satisfy the compiler. If you don't know what a VLA is nor what its advantages or disadvantages are you got a problem. Asking for that is absolutely appropriate.RobertS supports Monica Cellio– RobertS supports Monica Cellio2020-07-06 07:57:14 +00:00Commented Jul 6, 2020 at 7:57
2 Answers
How to make compiler compatible to c99 standard?
By default, the compiler defaults to the most compatible version of C version is installed. Do define the compilation version explicitly, compile the program with the following command-line:
$ gcc -std=c99 -o my_program my_program.c
By defining the -std=c99, the compiler will be using C99 standard.
Edit: If you're still getting the warning and not the error, then you need to provide your code to know what exactly is wrong.
11 Comments
-std=c99 flag must work correctly. Also, edit the question and add all the information after typing gcc -v command in your terminal.-std=c99 the warning won't go away. The compiler is just hinting."How to make a compilation successful for a program with a variable length array? (currently, Showing error : Variable sized array)."
Usually it isn't an error to compile a code with a VLA unless you compile with -Werror flag.
The diagnostic you get is high-probably "only" a warning that you use a VLA inside of it, which is risky.
Thus, the compiler informs you about that.
So, you indeed can compile a program with VLAs without any error.
If you got errors they must belong to anything else. We can't find those out since you showed no specific code.
Take a look at this question of mine, not so long ago (even if it is for Clang, it covers the same topic as the answers suggest that a compiler is free to complain about whatever it likes):
Why does clang complain about using variable-length arrays with '-std=c99' flag?
All useful information you can find there.
VLAs are not portable. Try to use alternatives, for example dynamically allocated arrays by using malloc().
Related:
"How to make compiler compatible to (the) C99 standard?"
As Rohan in his answer already said, you can use the -std-c99 flag at the invocation of gcc for that. But it probably won't solve your problem to do so.