C++

C++ Error Initializer Element is not Constant

In this article, we will be discussing a compiler error that a programmer often gets while coding in C++ Language: the “Initializer Element is not Constant”. To understand the error first, we need to understand the background of initializers and global variables. So, global variables are those variables that a programmer defines at the top of the code outside the functions. A variable that is declared globally will hold the value that is stored in it throughout the execution of code and will not lose it once a function is executed.

The advantage of the global declaration of variables is that they can be used in any function and any part of code. This means, we can use them and their values throughout our code. It is necessary for C++ programming that global variables should be initialized with constant expressions. If a programmer or coder forgets or is unable to do so, it will cause the error being discussed here. This error is also caused if static variables are also not declared. So, to avoid such an error, one must pass a constant value while initializing the global variable or pass them 0. So, this is a rule defined by C++ language. If this situation takes place, it will cause this error. So, to fix this type of error, we can declare variables in a function or initialize them with a constant value.

Syntax:

This is not a built-in function or library. So, it does not have a standard function to call it. Instead, it is an error so it will have a message that will be returned if we encounter such error.

error: initializer element is not constant

The above shown is the error message. It might change in some cases but the context of the message will remain the same.

Example # 01:

Now, to get a better hold of this error and to understand this error in more detail we will perform a practical example. To do, so we have initialized global variables “a” and “b”. The value of variable a is initialized to be 100. The value of b is equal to a. This means that whatever the value of a will be it will be the same as the value of b. In our main function, we have printed the value of b with the message “The value of b=”. We have not written any complex code here just to make our point clear. In line 10 we have returned 0 because our main function has an integer as its return type. Now, we will execute our code to see what happens.

#include <iostream>

using namespace std;
int a = 100;
int b = a;
int main()
{
    cout<<"The value of b="<<b<<endl;

    return 0;
}

After executing our code, we do not have the desired output. Instead of our output, the compiler has given us an error which is "initializer element is not constant". After that, it showed us the code in which the error occurred and that is line number 3. Now, if we recall our code, we initialized the value of integer b in line 3 which means that there is a problem with that piece of code. Let us try to resolve that problem.

To resolve the problem in line 3, we have re-checked our code and we have diagnosed the problem. We discussed earlier in the introduction that this error occurs when we do not declare a variable that we declare globally in our code or do not pass a constant value to it. The same case has occurred here. We have passed the value of integer a to b. The integer a is a variable, not a constant, therefore its value could change, So the compiler has shown us the error.

We have initialized the value of b to 100. After that, we executed the code, and the code was compiled successfully. So, by this example, we have made the point clear that we cannot pass a dynamic value to a global variable.

Example # 02:

This is a little bit complex example as compared to the previous one but the purpose of doing a complex example is to make you understand the error in more detail. To perform it, we have declared four integers that are x, y, z, and i. The values of the x, y, and z variables are 10, 20, and 50 respectively while “i” is an integer that will not be used to perform any operation. But it will be used as an iterator for our loop which we will be using in our main method. We also can declare this integer in our main method, but we found it easy, so we declared it here.

You can declare it in the main function or in a for loop. After that, we declared an integer pointer that is equal to x. In line 6 we have declared an array which is also globally declared and passed it three values because it has a length of 3. In our main method, we have written a loop that will run three times and will print the address of the indexes of the array and the values stored in that index of the array. Now, we will execute our code to see our output.

#include <iostream>

using namespace std;
int a = 10, y = 20, z = 50, i;
int *p = &x;
int *arr[3] = {p, &y, &z};
int main()
{
    for(i = 0; i<3; i++){
        cout<<"Address ="<<arr[i]<<"value ="<<*arr[i]<<endl;
    }


    return 0;
}

After the execution of our code, the compiler has given us the error which means that our code is not compiled successfully. If we take a look at the message of our error, we can see that we have the same error as in the previous example. This time it has occurred in line 6 of our code. If we look at our code and check line 6, we are initializing and declaring our array in line 6. So there has been a mistake in declaring or initializing our array. If we look at the initializing part, we can see that we have passed the variables y and z according to the programming standards, but we have passed x through a pointer. The pointer is not a constant variable and its value might change. So, the code has given an error.

To resolve the error, we will pass the integer x directly and see what happens.

The code has successfully been executed returning the address and values of the indexes of our array.

Conclusion

In this article, we discussed an error which is “Initializer Element is not correct”. We discussed the basics of global variables and how to initialize them according to coding rules and standards. We also performed multiple examples to get this error and resolved them.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.