error: variable-sized object may not be initialized

Have you ever come across this error?? 

"Understand the error thoroughly, so that it is not required to google in the future" - That's what I say to myself.

#include <stdio.h>
#include <string.h>

int main()
{   
    int n = 15;
    char arr[n] = "Stunning Palace";
    printf("Arr [%s] and sizeof(arr) [%d] strlen(arr) [%d]\n", arr, 
            sizeof(arr), strlen(arr));
    
    return 0;
}

Output:
1
2
In function 'main':
Line 7: error: variable-sized object may not be initialized

This error says,
1. Initialization has a problem.
2. Variable-length array has a problem.

The reason for the above error is we initialized the variable-length array which is not recommended.

char ARR_SIZE  = 15;
char arr[ARR_SIZE];
char arr[ARR_SIZE] = {};  //error
char arr[ARR_SIZE] = {0}; //error

An array size can't be assigned by another initializer (n = 15), but if the size is assigned by an initializer, it becomes a variable-length array. These variable-length arrays are prohibited from being initialized.

#include <stdio.h>
#include <string.h>

int main()
{   
    char ARR_SIZE  = 15;
    char arr[ARR_SIZE] = {};
    
    printf("Arr [%s] and sizeof(arr) [%d] strlen(arr) [%d]\n", arr, 
            sizeof(arr), strlen(arr));
    
    return 0;
}

Output:
1
2
In function 'main':
Line 7: error: variable-sized object may not be initialized


#include <stdio.h>
#include <string.h>

int main()
{   
    char ARR_SIZE  = 15;
    char arr[ARR_SIZE] = {0};
    
    printf("Arr [%s] and sizeof(arr) [%d] strlen(arr) [%d]\n", arr, 
            sizeof(arr), strlen(arr));
    
    return 0;
}

Output:
1
2
3
4
In function 'main':
Line 7: error: variable-sized object may not be initialized
Line 7: warning: excess elements in array initializer
Line 7: warning: (near initialization for 'arr')


The type of entity to be initialized shall be an array of unknown size or an object type that is not a variable-length array type, so the problem is with initialization. 

This issue can be solved by two-ways.

1. The type of entity to be initialized shall be an object type that is not a variable-length array type.
#include <stdio.h>

int main()
{   
    int n = 15;
    char arr[n];

    sprintf(arr, "Stunning Palace");
    printf("Arr [%s] and sizeof(arr) [%d] strlen(arr) [%d]\n", arr, 
            sizeof(arr), strlen(arr));
    
    return 0;
}

Output:
1
Arr [Stunning Palace] and sizeof(arr) [15] strlen(arr) [15]


Another way of assigning array size is,

#include <stdio.h>
#define ARR_SIZE 15

int main()
{   
    char arr[ARR_SIZE] = "Stunning Palace";
    printf("Arr [%s] and sizeof(arr) [%d] strlen(arr) [%d]\n", arr, 
            sizeof(arr), strlen(arr));
    
    return 0;
}

Output:
1
Arr [Stunning Palace] and sizeof(arr) [15] strlen(arr) [15]

2. The type of entity to be initialized shall be an array of unknown size.

#include <stdio.h>
#include <string.h>

int main()
{
   char arr[] = "Stunning Palace";

    printf("Arr %s and sizeof(arr) %d strlen(arr) %d\n", arr, sizeof(arr), strlen(arr));
    
    return 0;
}

Output:
1
Arr Stunning Palace and sizeof(arr) 16 strlen(arr) 15

Hence, the problem is with initialization with variable-length and not a declaration.

This problem is applicable to integer arrays as well.

#include <stdio.h>

int main()
{
    int n = 6;
    int arr[n] = {1, 2, 3, 4, 5, 6};

    printf("Array before memset() arr %d and sizeof(arr) %d \n", arr[5], sizeof(arr));
    
    return 0;
}

Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
In function 'main':
Line 6: error: variable-sized object may not be initialized
Line 6: warning: excess elements in array initializer
Line 6: warning: (near initialization for 'arr')
Line 6: warning: excess elements in array initializer
Line 6: warning: (near initialization for 'arr')
Line 6: warning: excess elements in array initializer
Line 6: warning: (near initialization for 'arr')
Line 6: warning: excess elements in array initializer
Line 6: warning: (near initialization for 'arr')
Line 6: warning: excess elements in array initializer
Line 6: warning: (near initialization for 'arr')
Line 6: warning: excess elements in array initializer
Line 6: warning: (near initialization for 'arr')

Integer array initialization also can be solved by the above two methods.




Comments