ISO C90 forbids variable length arrays

I am dynamically calculating the size of the array. Sort of:

void foo(size_t limit)
{
  char buffer[limit * 14 + 1];
}

But only the GCC compiler says:

error: ISO C90 forbids variable length array ‘buffer’

search on SO I found this answer :

C99 §6.7.5.2:

If size is an expression that is not an integer constant expression ...... every time it is evaluated, it must have a value greater than zero.

So, I re-declared the variable of type size limit:

void foo(const size_t limit)

But he continues to warn me. Is this a GCC bug?

+3
source share
6 answers

const - (. C99 6.6 §6 ), C99 .

, const -qualify , , , .

:

  • C99 -std=c99 -std=gnu99
  • malloc()
  • alloca(), , C90
  • , , limit

, C99 , , const -qualification: , , , , , , .

+6

c90 variavble. c99 gcc ompiler, .

c90 gcc, c99 spec:)

+4

const C, , .

#define SIZE 16
char bla[SIZE];   // not a variable length array, SIZE is a constant

const int size 16;
char bla[size];   // C99 variable length array, size is not constant
+3

, . VLA C90.

const size_t limit

. - 666.

, C ++.

const int i = 666;

C. , #define C.

+2
source

As written in your question, this is from C99, not C90, you need to compile it with C99 in order to be able to use variable-length arrays.

0
source

A constqualified variable is not an integer constant expression in the sense of a standard. It must be a literal constant, an enumeration constant, sizeofor some expression composed with them.

Go to C99 if you can. -std=c99Gcc option (or gnu99 if you want the gnu extension.)

0
source

All Articles