0

I cannot seem to compile my code when initializing a variable-sized array (code below). The thing is the code below is a sample code from university lecture notes, so it should work perfectly fine but it doesn't.

int MAX = 20;
char input[MAX] = {'\0'};

I saw a question with similar title and the person stated that he can only compile when he use constant variable as the size of array. So I tried writing the code below but i still get the same compile error.

const int MAX = 20;
int main() {
    char input[MAX] = {'\0'};

What's more confusing is that when i try to hard-code the size of the array (code below) it works. I don't understand why and what is the problem with the codes above when to me it looks the same with the code below.

char input[20] = {'\0'};

Is it a problem with the compiler? Or is this syntax not really allowed in C?

4
  • So what is the problem? No, you can't initialize a variable sized array. Commented May 6, 2020 at 13:18
  • What is your error? Commented May 6, 2020 at 13:19
  • gcc says "variable-sized object may not be initialized". Commented May 6, 2020 at 13:19
  • Don't use variable sized arrays in C. There is malloc for this purpose. Otherwise, choose another language - Python, Ruby, etc. Commented May 6, 2020 at 13:25

3 Answers 3

2

Variable-length arrays (VLA) cannot have an initializer list, because initializer lists are computed at compile-time, but the VLA doesn't have a size until run-time.

Even if you const qualify the variable MAX, it is still not an integer constant expression and therefore input is still a VLA instead of an ordinary array. Should you do #define MAX 20 instead, then the size is an integer constant expression and you get a regular array instead of a VLA.

The fix is trivial, just zero the array in run-time instead:

int MAX = 20;
char input[MAX];
memset(input, '\0', MAX);
Sign up to request clarification or add additional context in comments.

Comments

1
int MAX = 20;
char input[MAX] = {'\0'};

input is a variable length array (VLA) and VLAs can't be initialized in standard C. Even if you const qualify MAX, it's still not a constant expression and thus input is still a VLA and thus it can't be initialized.

I saw a question with similar title and the person stated that he can only compile when he use constant variable as the size of array

That's probably said for C++. Because in C++ const-qualified variables are treated as constant expression. But that's not the case in C (this is mainly a historical discrepancy).

However,

char input[20] = {'\0'};

is an array and 20 is a constant expression and thus input can be initialized.

You could instead use:

  • a preprocessor macro (#define MAX 20)
  • an enum constant (e.g. enum { MAX = 20};)

that would qualify as a constant expression in C.

A general note on VLAs: beware that VLAs can be dangerous if you allocate a large arrays as they're typically allocated on stack and it could overflow. And there's no way to detect that overflow. So you'd want to avoid VLAs whenever you can (unless it's for a very small array, like in your example).

Comments

1

Although the value of MAX is known at compile time, the above definitions of input actually define variable sized arrays, and as per the C Standard, variable sized arrays cannot have an initializer, nor can they appear at global scope.

Some C compilers might allow an initializer as an extension, but you should not rely on this being a general case.

You can still use a name for the array size, but using the preprocessor with a macro:

#define MAX  20
char input[MAX] = {'\0'};

int main() {
    char output[MAX] = {'\0'};
    ...

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.