Question Details

No question body available.

Tags

c variable-length-array function-parameter

Answers (2)

March 26, 2026 Score: 2 Rep: 235,683 Quality: Medium Completeness: 60%

Can you pass the size of an variable length array, VLA, as an element in a large struct and use it as size for the VLA?

Of course you can pass the size. The size is merely a value, and you can pass values.

You can use the size. The size is merely a value, and you can use values.

However, what does it mean to “use it as size for the VLA”? In what code do you need a size for the VLA?

Let’s look at your declaration:

void foo(largeStructType largeStructPtr, int arr[largeStructPtr->size])

This declares arr to be a pointer to an int. It looks like it declares arr to be an array, but it actually declares arr to be a pointer to an int because clause 6.7.7.4 of the C 2024 standard says:

… A declaration of a parameter as “array of type” shall be adjusted to "qualified pointer to type", where the type qualifiers (if any) are those specified within the [ and ] of the array type derivation…

So we do not care that this function declaration looks like it declares arr to be a variable-length array, because it does not, in the end, declare an array, just a pointer to an int. You could also write the declaration either of these two ways, and they would be equivalent to the original:

void foo(largeStructType largeStructPtr, int arr[]) …
void foo(largeStructType largeStructPtr, int arr) …

Inside the function, there is no variable length array from this declaration. arr is a pointer, and you can use it to access elements of the original array using arr[i].

If you use the size of the array inside the function to control your indexing into the array, so that all the indices are within proper bounds, then that is fine. You could also use the size inside the function to describe a variable length array, instead of just a pointer, but you probably do not need to do that. If you think you do, ask a new question describing what your needs are.

You could also write the function so the parameter is a pointer to a variable length array:

void foo(largeStructType largeStructPtr, int (arr)[largeStructPtr->size]) …

In this case, arr is not adjusted—it is already a pointer, not an array, so there is no adjustment of an array to a pointer. It points to an array, but the automatic adjust is just for direct arrays, not for pointers to arrays.

However, passing a pointer to a variable length array is not often needed in code, and you should not use it unless there is some particular need.

March 26, 2026 Score: 1 Rep: 313,444 Quality: Medium Completeness: 60%

In this function declaration

void foo(largeStructType largeStructPtr, int arr[largeStructPtr->size])

the declaration of the second parameter indeed declares a variable length array because 1) the array declaration is not incomplete (for a parameter of an incomplete variable length array in a function prototype there is used the symbol '') and 2) has a non-constant expression.

From the C24 Standard (6.7.7.3 Array declarators):

4 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 as part of the nested sequence of declarators or abstract declarators for a parameter declaration, not including anything inside an array size expression in one of those declarators;160) 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 with automatic storage duration are a conditional feature that implementations may support; see 6.10.10.4.)

It is unimportant that the compiler will adjust the parameter to a pointer of the array element type because before adjusting the array to a pointer compilers check whether the array declaration is correct . That is the declaration of the parameter itself declares a variable length array as any other declaration of a variable length array.

For example, if you will declare a function with a not variable length array like

void f( int a[-1], size_t n );

then even compilers adjust parameters of array types to pointers to array element types and seems should ignore the constant integer expression in the array declaration shown above nevertheless compilers (must) issue an error like

error: size of array a is negative

because the array size may not be negative.

An implementation may conditionally support variable length arrays. As far as I know for example the C compiler of MS Visual Studio does support variable length arrays.

Here is a demonstration program.

#include 

void display( size_t, int [] ); void display( sizet n, int a[n] ) { for ( sizet i = 0; i < n; i++ ) { printf( "%d ", a[i] ); } putchar( '\n' ); }

int main(void) { int a[] = { 0, 1, 2, 3, 4, 5 }; display( sizeof( a ) / sizeof( a ), a ); }

Its output is

0 1 2 3 4 5 

As you can see the function parameter in the both function declarations (that declare the same one function) is declared as a variable length array. Even if the first function declaration will be removed nevertheless in the second function declaration the second function parameter still declares a variable length array.

A declarator of a parameter declared as a variable length array can contain an expression that refers to preceding function parameters.

A compiler that does not support variable length arrays will not compile the above program.

By the way you could declare the function without a variable length array due to adjusting the second parameter to a pointer of the type int . The only benefit of your initial function declaration is its descriptive characteristic.

The function declaration could look at least like

void foo(largeStructType largeStructPtr, int arr[]);

or

void foo(largeStructType largeStructPtr, int *arr);

and within the function you could use the expression largeStructPtr->size to determine the number of elements in the passed array.