5

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

1
  • 2
    The first snippet doesn’t attempt to initialize the array, so there’s no error to diagnose. Commented Mar 10, 2021 at 14:12

2 Answers 2

6

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the answer. But is the memory malloc-ed in this case?
@user1570891 No, variable length arrays have automatic storage duration.
4

This definition:

char arr[diff];

Creates a variable length array. Such an array has its size determined at runtime. Because of this, it may not be initialized.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.