I have the next two code examples:
const char *val = strchr(ch, ' ');
const int diff = (int)(val - ch);
char arr[diff];
and
const char *val = strchr(ch, ' ');
const int diff = (int)(val - ch);
char arr[diff] = {0};
The second one generates the error like
error: variable-sized object may not be initialized
It is correct error and I understand why it happens.
I wonder why the first code snippet doesn't generate the error?
Update: Also regarding sizeof(arr) at first snippet gives the size of array, but I thought that sizeof is a compile time operator (?)
But, unlike the normal arrays, variable sized arrays cannot be initialized. For example, the following program compiles and runs fine on a C99 compatible compiler. But the following fails with compilation error.
An array can also be initialized using a loop. The loop iterates from 0 to (size - 1) for accessing all indices of the array starting from 0. The following syntax uses a “for loop” to initialize the array elements. This is the most common way to initialize an array in C. // declare an array. // initialize array using a "for" loop.
If you access any uninitialized array value, then just like any uninitialized variable, it will give you a garbage value. An array can be initialized at the time of its declaration. In this method of array declaration, the compiler will allocate an array of size equal to the number of the array elements.
How to initialize an array? There are two ways to initialize an array. Static array initialization - Initializes all elements of array during its declaration. Dynamic array initialization - The declared array is initialized some time later during execution of program.
In the second case you are trying to initialize a variable length array (because the size of the array is not specified with an integer constant expression; the presence of the qualifier const
in the declaration of the variable diff does not make it an integer constant expression in the array declaration)
char arr[diff] = {0};
that is not allowed for variable length arrays.
From the C Standard (6.7.9 Initialization)
3 The type of the entity to be initialized shall be an array of unknown size or a complete object type that is not a variable length array type
You could set all elements of the array to zero the following way
#include <string.h>
//...
char arr[diff];
memset( arr, 0, diff );
As for the operator sizeof
then for variable length arrays it is calculated at run-time.
From the C Standard (6.5.3.4 The sizeof and alignof operators)
2 The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand. The result is an integer. If the type of the operand is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With