Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C initialized and non initialized array with variable size

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 (?)

like image 666
user1570891 Avatar asked Mar 10 '21 14:03

user1570891


People also ask

Can variable sized arrays be initialized in C?

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.

How to initialize an array in C with a loop?

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.

What happens if you access an array value that is not initialized?

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 in Java?

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.


Video Answer


1 Answers

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

like image 96
Vlad from Moscow Avatar answered Sep 18 '22 22:09

Vlad from Moscow