2

Can I use a variable to define an array's size?

int test = 12;
int testarr[test];

Would this work, I don't want to change the size of the array after initialization. The int test's value isn't known at compile time.

  • 1
    On C99 and above this is called a VLA and it is valid, if you don't want VLAs you can use dynamic memory: `int testarr[test];` --> `int *testarr = malloc(sizeof *testarr * test);` – David Ranieri Jun 25 '20 at 08:51
  • This would be what is called a VariableLengthArray (VLA) and is supported in some versions of the C standard. It has been made an optional feature in the newest versions, though. – Hulk Jun 25 '20 at 08:55
  • Does this answer your question? [malloced array VS. variable-length-array](https://stackoverflow.com/questions/16672322/malloced-array-vs-variable-length-array) – Hulk Jun 25 '20 at 09:01
  • C99 must have VLAs, [C11 made them optional](http://port70.net/~nsz/c/c11/n1570.html#6.10.8.3) – pmg Jun 25 '20 at 09:21

5 Answers5

4

From C99 it is allowed but only for the automatic variables.

this is illegal:

int test = 12;
int testarr[test];   // illegal - static storage variable

int foo(void) 
{
    int test = 12;
    static int testarr[test];   // illegal - static storage variable
}

the only valid form is:

int foo(void) 
{
    int test = 12;
    int testarr[test];   // legal - automatic storage variable
}
Lundin
  • 195,001
  • 40
  • 254
  • 396
0___________
  • 60,014
  • 4
  • 34
  • 74
  • For confirmation, is the explanation about C99? – Rohan Bari Jun 25 '20 at 09:31
  • @RohanBari it is correct for C99 and those implementations of newer versions that include VLAs as an optional feature. One major compiler that never supported it is MSVC, see https://stackoverflow.com/questions/5246900/enabling-vlas-variable-length-arrays-in-ms-visual-c – Hulk Jun 25 '20 at 09:36
2

can i use an variable to define an arrays size?

This is called Variable Length Arrays (VLA).

Read Modern C then the C11 standard n1570 (and see this reference). VLAs are permitted in §6.7.6; also read the documentation of your C compiler (e.g. GCC).

But you don't want to overflow your call stack, typically limited to a megabyte on laptops (and OS specific).

So you may prefer C dynamic memory allocation with e.g. malloc(3) (and free ...) or calloc. Beware of memory leaks.

You might be interested by tools such as valgrind. You need to learn to use your debugger (e.g. GDB).

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
2

Can I use an integer variable to define an arrays length?

Yes, that is what is called a variable length array and is part of the C standard since C99. Note that an implementation does not need to support it. Therefore you may prefer dynamic memory allocation. Take a look at here:

malloced array VS. variable-length-array

To cite the C standard:

"If the size is not present, the array type is an incomplete type. If the size is * instead of being an expression, the array type is a variable length array type of unspecified size, which can only be used in declarations or type names with function prototype scope;146) such arrays are nonetheless complete types. If the size is an integer constant expression and the element type has a known constant size,the array type is not a variable length array type; otherwise, the array type is a variable length array type. (Variable length arrays are a conditional feature that implementations need not support; see 6.10.8.3.)"

"146) Thus, * can be used only in function declarations that are not definitions (see 6.7.6.3)."

Source: C18, 6.7.6.2/4

Also note:

"Array objects declared with the _Thread_local, static, or extern storage-class specifier cannot have a variable length array (VLA) type."

Source: C18, 6.7.6.2/10


VLAs cannot be used:

  • at file scope.
  • when qualified with _Thread_local, static, or extern storage-class specifier.
  • if they have linkage.

Which means that they can only be used at function-scope and when of storage-class automatic, which is done by default when omitting any explicit specifier.

Feel free to ask for further clarification if you don't understand something.

  • *`VLAs cannot be used:`* which effectively makes them only allowed for the automatic storage variables. – 0___________ Jun 25 '20 at 10:14
  • I love beginners questions answers which are full of standard citing (interesting if the asker understands anything from it), which IMO do not expain anything. – 0___________ Jun 25 '20 at 10:16
  • 1
    @P__J__ I think it explains it very well even if maybe not everything is understood at the first read. Why not cite the rules we all do care about? If anything is misunderstood, OP can ask for more clarification. – RobertS supports Monica Cellio Jun 25 '20 at 10:34
0

Without knowing much of the context, wouldn't it be easier to just do this?

#define TEST 12  //to ensure this value will not change at all
int testarr[TEST];

Technically your method should work too but the value of test may change later on depending on the piece of code you written

-4

So, when you try this on C, as in this [https://onlinegdb.com/rJzE8yzC8][1]

It successfully compiles and you can also update the value of the variable int test

However, after the update of test, the size of array does not change since arrays are static-defined.

For guarantee, I suggest you to use either const int or macro variables for doing this.

thisguy
  • 39
  • 6